简体   繁体   English

如何从Firebase检索数据并输入到CSV

[英]How to retrieve data from firebase and input to csv

I have a problem with retrieve data from firebase. 我从Firebase检索数据时遇到问题。 I want all the data to be inputted in csv. 我希望所有数据都输入到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 从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 将数据保存为内部存储文件格式.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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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