简体   繁体   中英

How to retrieve data from firebase and input to csv

I have a problem with retrieve data from firebase. I want all the data to be inputted in csv. The problem is "only one data inputted". Anyone, please help me fix the problem.

thanks!

Database Reference

    firebaseAuth = FirebaseAuth.getInstance();
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseProduct = firebaseDatabase.getReference(firebaseAuth.getUid()).child("Product");
    itemsList = new ArrayList<>();

Retrieve Data from Firebase

    downloadButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            databaseProduct.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    itemsList = new ArrayList<>();

                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        String getDate = snapshot.child("productDate").getValue().toString();
                        String getInspector = snapshot.child("inspectorName").getValue().toString();
                        String getLocation = snapshot.child("marketLocation").getValue().toString();
                        String getProductName = snapshot.child("productName").getValue().toString();
                        String getPrice = snapshot.child("productPrice").getValue().toString();
                        String getAmount = snapshot.child("productQuantity").getValue().toString();
                        String getCode = snapshot.child("barCode").getValue().toString();

                        exportCSV(getDate, getInspector, getLocation, getProductName, getPrice, getAmount, getCode);
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    });

Save Data into Internal Storage file format .csv

public void exportCSV(String getDate, String getInspector, String getLocation, String getProductName, String getPrice, String getAmount, String getCode) {
    Calendar calendar = Calendar.getInstance();
    String currentDate = DateFormat.getDateInstance().format(calendar.getTime());
    isStoragePermissionGranted();

    try {
        File root = new File(Environment.getExternalStorageDirectory() + "/StockCount/");

        if (!root.exists()) {
            root.mkdirs();
        }

        File myCSV = new File(root, currentDate + " DataStockCount.csv");
        myCSV.createNewFile();

        FileWriter writer = new FileWriter(myCSV);
        writer.append("Date : " + getDate + "\n");
        writer.append("Inspector : " + getInspector + "\n");
        writer.append("Location : " + getLocation + "\n");
        writer.append("Product Name : " + getProductName + "\n");
        writer.append("Price : " + getPrice + "\n");
        writer.append("Quantity : " + getAmount + "\n");
        writer.append("Barcode : " + getCode + "\n");
        writer.flush();
        writer.close();

        Toast.makeText(this, "File Saved Successfully!", Toast.LENGTH_LONG).show();

    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "Error : " + e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

Add comma after each column data and new line after each row.

writer.append("Date : " + getDate + ",");
writer.append("Inspector : " + getInspector + ","); 
 writer.append("Location : " + getLocation + ","); 
 writer.append("Product Name : " + getProductName + ",");     
 writer.append("Price : " + getPrice + ",");
writer.append("Quantity : " + getAmount + ","); 
 writer.append("Barcode : " + getCode + "\n");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM