簡體   English   中英

如何從Firebase檢索數據並輸入到CSV

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

我從Firebase檢索數據時遇到問題。 我希望所有數據都輸入到csv中。 問題是“僅輸入一個數據”。 任何人,請幫助我解決問題。

謝謝!

數據庫參考

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

從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) {

                }
            });
        }
    });

將數據保存為內部存儲文件格式.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();
    }
}

在每列數據之后添加逗號,並在每行之后添加新行。

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