【问题标题】:How to update arrayList in cloud firestore in android如何在 android 的 Cloud Firestore 中更新 arrayList
【发布时间】: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


    【解决方案1】:

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

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

    根据 API 文档:

    返回一个可以与 set() 或 update() 一起使用的特殊值 告诉服务器将给定元素与任何数组值联合 服务器上已经存在。每个指定的元素不 已经存在的数组将被添加到末尾。如果场 被修改的还不是一个数组,它将被覆盖 完全包含指定元素的数组。

    This is described in more detail the documentation.

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

    【讨论】:

    • 感谢您的回复,我想知道是否可以在其中添加更多 java POJO 对象的数组列表。这样我就可以尽力而为。
    • 如果你想使用 POJO,你必须先从文档中读取数据,在内存中对其进行修改,然后再将其写回。如果你只是想添加项目,使用 FieldValue.arrayUnion() 效率更高。
    猜你喜欢
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2019-03-28
    • 2019-02-14
    相关资源
    最近更新 更多