【发布时间】:2018-12-11 18:20:46
【问题描述】:
使用 Cloud Firestore 时,始终牢记阅读文档的数量是一件好事。我正在开发一个库存应用程序,其中为产品列表生成账单以及其他详细信息。除了账单中的arraylist 更新外,一切正常。我可以在账单中插入产品的数组列表以及其他详细信息,但以后无法更新数组列表。每当我尝试在文档中插入更多的arraylist 时,旧的arraylist 就会被删除。我不明白如何在该数组列表中插入更多条目。
private void loadDatatoFirestore() {
//product details entry
final ArrayList<OrderedProductModel> newProductModel = new ArrayList<>();
for (int i=0; i<productModelList.size(); i++){
OrderedProductModel model = new OrderedProductModel(productModelList.get(i).getProductNumber(),
productModelList.get(i).getProductName(),
productModelList.get(i).getOrderedQuantity(),
productModelList.get(i).getSellingPrice());
newProductModel.add(model);
}
BillModel insertBill = new BillModel(billNumber, partyName, date, totalPrice, BILL_STATUS, newProductModel);
db.collection("mainCollection").document("BillDocument")
.collection("BillCollection")
.document(billNumber)
.set(insertBill, SetOptions.merge())
.addOnSuccessListener(new OnSuccessListener<Void>() {...}
上面的代码工作正常.. 但现在我想在 orderderProduct 数组中插入更多条目。 我正在尝试像这样解决它,但随后会发生编译时错误。我不知道如何在这里更新数组。
private void updateBill(String billNumber) {
//here i am trying to insert dummy data
ArrayList<OrderedProductModel> testArray = new ArrayList<>();
testArray.add(new OrderedProductModel("9999","testprod1",50,40));
testArray.add(new OrderedProductModel("9911","testprod2",70,60));
BillModel billtest = new BillModel(testArray);
db.collection("mainCollection").document("BillDocument")
.collection("BillCollection")
.document(billNumber)
.update(billtest,SetOptions.merge())
.addOnSuccessListener(new OnSuccessListener<Void>() {....}
BillModel
public class BillModel {
String billNumber;
String partyName;
String billingDate;
float totalPrice;
String status;
ArrayList<OrderedProductModel> orderderProduct;
public BillModel(ArrayList<OrderedProductModel> orderderProduct) {
this.orderderProduct = orderderProduct;
}
public BillModel(String billNumber, String partyName, String billingDate, float totalPrice, String status, ArrayList<OrderedProductModel> orderderProduct) {
this.billNumber = billNumber;
this.partyName = partyName;
this.billingDate = billingDate;
this.totalPrice = totalPrice;
this.status = status;
this.orderderProduct = orderderProduct;
}
OrderedProductModel
public class OrderedProductModel {
private String productNumber;
private String productName;
private int orderedQuantity;
private float sellingPrice;
public OrderedProductModel(String productNumber, String productName, int orderedQuantity, float sellingPrice) {
this.productNumber = productNumber;
this.productName = productName;
this.orderedQuantity = orderedQuantity;
this.sellingPrice = sellingPrice;
}
正在附加图片...
firestore image
bill creation image
【问题讨论】:
标签: android firebase google-cloud-firestore