我在recyclerview中一直只获得第一项,但是在logcat中我将其全部获得

时间:2018-06-20 12:17:35

标签: android android-recyclerview google-cloud-firestore recycler-adapter

这是我的代码(我认为问题出在 notifydatasetchanged 及其应放置的位置)

'

这是我的消防库集合:

enter image description here

https://i.stack.imgur.com/j868j.jpg

1 个答案:

答案 0 :(得分:0)

还不能解决您的实际问题,而只是一些基本的代码清除(我无法在注释中添加内容)。

确定对子集合的引用的方法过于复杂。实际上,您可以只在第一个循环中从Document获取子集合:

FirebaseFirestore db = FirebaseFirestore.getInstance();

CollectionReference jobOffersCollection = db.collection("Joboffers");

jobOffersCollection.addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
        for (DocumentChange snapshot : documentSnapshots.getDocumentChanges()) {
            if (snapshot.getType()==DocumentChange.Type.ADDED) {
                Log.d("employerid", snapshot.getDocument().getId());
                CollectionReference offerCollection = snapshot.getDocument().getReference().collection("Offers");
                offerCollection.addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {    
                    for (DocumentChange offerSnapshot : documentSnapshots.getDocumentChanges()) {
                        if (n.getType() == DocumentChange.Type.ADDED) {
                            JO jo = snapshot1.getDocument().toObject(JO.class);

                            Log.d("poste", jo.getPoste().toString());
                            postelist.add(jo);
                            jolistadapter.notifyDataSetChanged();
                        }
                    }
                }
            });
        }
        MList.setAdapter(jolistadapter);
    }
});

请注意,我不完全理解您为什么以自己的方式做事。我建议:

  • 使用get代替addSnapshotListener,因为您的代码似乎只专注于一次性执行操作。
  • documentSnapshot.getDocuments()而不是getDocumentChanges()上进行循环,这将消除对非常不寻常的DocumentChange.Type.ADDED检查的需要。