Firestore从集合中获取数组

时间:2018-02-03 04:52:50

标签: java android firebase google-cloud-firestore

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

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

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

用于获取数据的Java代码:

mFirestore.collection(" catend")。addSnapshotListener(new EventListener(){         @覆盖         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() {
        }
    }
}

调试结果:

You can see in the images

Firebase structure

Andoird studio log

2 个答案:

答案 0 :(得分:0)

I see in your Firestore response that the packs object is a List and not an array. I also see the same thing in your model class, List<Packs> packs.

The main idea is not to create a model class according with the data that you already have in your database. So a question such "How I can create model for this response?" is not a correct, because this is not how the things works. You create the model class in the first place and then you add data to the database in the way you have structured your helper class.

So the packs property from your document si actually a Map. To print those value out please change this line of code:

Log.d(TAG, "onEvent: packs"+productModel.getPacksList()); //not work

with

Map<String, Object> packs = (Map<String, Object>) documentSnapshot.get("packs");
Map<String, Object> zero = (Map<String, Object>) packs.get("0");
String price = (String) zero.get("price");
String subcode = (String) zero.get("subcode");
String weight = (String) zero.get("price");
Log.d(TAG, price + " / " + subcode + " / " + weight);

Your output will be: 12 / s1 / 1

Edit:

Looking again at your model class, the problem in your code is the getter. So if you want to use:

productModel.getPacksList();

Please change the following getter:

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

to

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

And then simply call:

productModel.getPacks();

That's it!

答案 1 :(得分:0)

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

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类,我在这里获取数据:

 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