gpt4 book ai didi

android - 如何在 android 中更新 cloud firestore 中的 arrayList

转载 作者:行者123 更新时间:2023-12-04 23:56:42 26 4
gpt4 key购买 nike

使用云 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>() {....}

账单模型

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

最佳答案

如果您想在 Firestore 中更新数组类型的内容,您不能简单地合并到一个新数组中。在合并期间,新字段值将始终完全替换旧字段值。

如果你想修改数组的内容,你必须使用特殊的FieldValue.arrayUnion()操作作为您字段的值。

根据 API 文档:

Returns a special value that can be used with set() or update() that tells the server to union the given elements with any array value that already exists on the server. Each specified element that doesn't already exist in the array will be added to the end. If the field being modified is not already an array it will be overwritten with an array containing exactly the specified elements.

This is described in more detail the documentation .

不幸的是,这与 Java POJO 对象发生的自动字段映射不兼容,因为您正在使用 BillModel。您必须转换代码以使用 Map 类型对象更新文档,为每个要修改的 BillModel 字段手动将值复制到其中。

关于android - 如何在 android 中更新 cloud firestore 中的 arrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53730163/

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