gpt4 book ai didi

java - 如何从 firebase 检索数据并输入到 csv

转载 作者:行者123 更新时间:2023-12-02 01:07:11 25 4
gpt4 key购买 nike

我在从 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");

关于java - 如何从 firebase 检索数据并输入到 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57720405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com