Firestore从集合中获取数组 [英] Firestore fetch array from collection

查看:164
本文介绍了Firestore从集合中获取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在firestore中有一个集合,它返回响应如下:

I have collection in firestore which return the response as follows:

{packs = [{subcode = s1,weight = 1,price = 12}],name = abc,desc = Good,code = 001}

{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=Good, code=001}

如何为此响应创建模型并在Android中解析此数据。
在当前模型中,它返回包null,我得到数据desc,名称和代码但包是null,包是一个数组。

How I can create model for this response and parse this data in Android. In the current model, It returns the packs null, I am getting the data desc, name and code but packs is null, packs is a array.

用于获取数据的Java代码:

Java code for fetching data:

mFirestore.collection(catend)。addSnapshotListener(new EventListener(){
@Override
public void onEvent (QuerySnapshot documentSnapshots,FirebaseFirestoreException e){

mFirestore.collection("catend").addSnapshotListener(new EventListener() { @Override public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

        if (e != null) {
            Log.d(TAG, "onEvent: error" + e.getMessage());
        }

        for (DocumentChange document : documentSnapshots.getDocumentChanges()) {

            switch (document.getType()) {
                case ADDED:

                    ProductTest productModel=document.getDocument().toObject(ProductTest.class);
                    Log.d(TAG, "onEvent: response"+document.getDocument().getData());
                    Log.d(TAG, "onEvent: code="+productModel.getCode());  //work
                    Log.d(TAG, "onEvent: description="+productModel.getDesc()); //work
                    Log.d(TAG, "onEvent: name="+productModel.getName()); //work

                    Log.d(TAG, "onEvent: packs"+productModel.getPacksList()); //not work
                    break;
                case REMOVED:
                    break;
                case MODIFIED:
                    break;
            }

        }

    }
});

模特课程:

public class ProductTest {
    String code,desc,name;
    List<Packs> packs;

    public ProductTest() {
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Packs> getPacksList() {
        return packs;
    }

    public void setPacksList(List<Packs> packsList) {
        this.packs = packsList;
    }

    public  class  Packs
    {
        String subcode;
        int price;

        public String getSubcode() {
            return subcode;
        }

        public void setSubcode(String subcode) {
            this.subcode = subcode;
        }

        public int getPrice() {
            return price;
        }

        public void setPrice(int price) {
            this.price = price;
        }

        public Packs() {
        }
    }
}

调试结果:

你可以在图片中看到

Firebase结构

Andoird工作室日志

推荐答案

Alex Mamo回答帮帮我很多,我改变了我的模型代码:

Alex Mamo answer help me alot, I changed my model code:

public class ProductTest {
    String code,desc,name;
    ArrayList<Object> packs;

    public ArrayList<Object> getPacks() {
        return packs;
    }

    public void setPacks(ArrayList<Object> packs) {
        this.packs = packs;
    }

    public ProductTest() {
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

我还更新了我的MainActivity.java我获取数据的类:

I also updated my MainActivity.java class where I am fetching data:

 mFirestore.collection("catend").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                if (e != null) {
                    Log.d(TAG, "onEvent: error" + e.getMessage());
                }
                for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
                    switch (document.getType()) {
                        case ADDED:
                            ProductTest productModel=document.getDocument().toObject(ProductTest.class);
                            Log.d(TAG, "onEvent: response"+document.getDocument().getData());
                            Log.d(TAG, "onEvent: code="+productModel.getCode());  //work
                            Log.d(TAG, "onEvent: description="+productModel.getDesc()); //work
                            Log.d(TAG, "onEvent: name="+productModel.getName()); //work
                            Log.d(TAG, "onEvent: packs"+productModel.getPacks()); //Work
                            for (int i = 0; i <productModel.getPacks().size() ; i++) {
                                try {
                                    JSONObject jsonObject=new JSONObject(productModel.getPacks().get(i).toString());
                                    Log.d(TAG, "onEvent: subcode= "+jsonObject.getString("subcode"));
                                    Log.d(TAG, "onEvent: subprice="+jsonObject.getString("price"));
                                    Log.d(TAG, "onEvent: weight="+jsonObject.getString("weight"));

                                } catch (JSONException e1) {
                                    e1.printStackTrace();
                                }
                            }

                            break;
                        case REMOVED:
                            break;
                        case MODIFIED:
                            break;
                    }

                }

            }
        });

输出:
Android studio logcat

这篇关于Firestore从集合中获取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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