【发布时间】:2019-08-30 18:39:54
【问题描述】:
我在从 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();
}
}
【问题讨论】:
-
通过包含代码而不是屏幕截图来编辑问题。
-
对不起,我无法分享代码,我收到错误“看起来您的帖子主要是代码;请添加更多详细信息。”
-
我把代码放在答案栏中,谢谢..
-
你写的不是CSV格式。
-
@BasilBourque 错了吗? File myCSV = new File(root, currentDate + "DataStockCount.csv"); myCSV.createNewFile();
标签: java android firebase firebase-realtime-database