如何在Android的Cloud Firestore中更新arrayList [英] How to update arrayList in cloud firestore in android

查看:29
本文介绍了如何在Android的Cloud Firestore中更新arrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Cloud Firestore始终要牢记要读取的文档数.我正在开发一个库存应用程序,在该应用程序中将为产品列表以及其他详细信息生成账单.除了更新条例草案中的arraylist之外,一切都工作正常.我可以在帐单中插入产品的arraylist以及其他详细信息,但以后无法更新arraylist.每当我尝试在文档中插入更多arraylist时,旧的arraylist都会被删除.我不明白如何在该arraylist中插入更多条目.

Working with cloud firestore its always good to keep in mind about count of document read. I am working on an inventory app in which bill is getting generated for the list of products along with other details. Everything is working fine except the update of arraylist in the bill. I am able to insert the arraylist of product along with other details in the bill but i am not able to update the arraylist later. Whenever i try to insert more arraylist into the document, old arraylist get deleted. I dont understand how to insert more entries into that 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数组中插入更多条目.我正在尝试像这样解决它,但随后发生了编译时错误.我不知道如何在这里更新数组.

the above code is working fine.. but now i want to insert more entries into orderderProduct array. I am trying to solve it like this but compile time error occurs then. I have no idea how to update array here.

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图片
清单创建图片

推荐答案

如果要在Firestore中更新数组类型的内容,则不能简单地合并到新数组中.在合并过程中,新的字段值将始终完全替换旧的字段值.

If you want to update the contents of an array type in Firestore, you can't simply merge in a new array. A new field value will always entirely replace an old field value during a merge.

如果要修改数组的内容,则必须使用特殊的

If you want to modify the contents of the array, you have to use the special FieldValue.arrayUnion() operation as the value for your field.

根据API文档:

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

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.

文档中对此进行了详细的介绍.

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

Unforunately, this is not compatible with the automatic field mapping that occurs with Java POJO objects, as you're using with BillModel. You'll have to convert your code to update the document with a Map type object instead, manually copying values into it for each of the BillModel fields you want to modify.

这篇关于如何在Android的Cloud Firestore中更新arrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆