【问题标题】:I keep getting only the first item in my recyclerview but in the logcat i get them all我一直只在我的 recyclerview 中获得第一项,但在 logcat 中我得到了所有
【发布时间】:2018-06-20 12:17:35
【问题描述】:

这是我的代码(我认为问题出在 notifydatasetchanged 以及应该放在哪里):

FirebaseFirestore db = FirebaseFirestore.getInstance();

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

db1.addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
        String empid = "";
        for (  DocumentChange snapshot : documentSnapshots.getDocumentChanges())
        {
            if (snapshot.getType()==DocumentChange.Type.ADDED) {
                empid = snapshot.getDocument().getId();
                Log.d("employerid",empid);

            }
            FirebaseFirestore db = FirebaseFirestore.getInstance();
            CollectionReference db2 =db.collection("Joboffers").document(empid).collection("Offers");
                    db2.addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                    for (DocumentChange snapshot1 : documentSnapshots.getDocumentChanges()) {
                        JO jo=new JO();
                        if (snapshot1.getType() == DocumentChange.Type.ADDED) {
                         jo = snapshot1.getDocument().toObject(JO.class);

                        String s=jo.getPoste().toString();
                        Log.d("poste",s);
                        postelist.add(jo);
                            jolistadapter.notifyDataSetChanged();
                    }

                    }
                }

            });


        }

        MList.setAdapter(jolistadapter);



    }


});

这是我的 Firestore 收藏:

【问题讨论】:

  • 你想达到什么目的?考虑添加代码的预期结果。
  • 我正在尝试查看我收藏中的所有文档,所以我使用了 2 个快照
  • 我在 logcat 中得到了我想要的结果,但我没有在我的回收站视图中得到它们,我只得到第一个文档
  • 这听起来很奇怪。如果你在postelist.add(jo) 上设置一个断点并在调试器中运行,你能看到它在列表中添加了额外的项目吗?
  • @FrankvanPuffelen,“Log.d”向我展示了“Poste”字段的值,但是在模拟器 recyclerview 中我只得到一个

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


【解决方案1】:

还没有回答你的实际问题,只是一些基本的代码清理(我无法在评论中加入)。

确定对子集合的引用的方式过于复杂。实际上,您可以在第一个循环中从 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 检查。

【讨论】:

  • ,"joboffers" 集合由许多文档组成,每个文档的 id 与保存它的雇主相同。但是,每个文件将包含 2 个集合(“offers”和“application”);第一个是雇主职位的 offer 列表,第二个(仍然未完成)将包含应用程序列表求职者。我正在尝试做的是将每个雇主的“offer”集合中的所有文件放入recyclerview。
猜你喜欢
  • 1970-01-01
  • 2021-12-17
  • 2023-03-18
  • 1970-01-01
  • 2012-07-29
  • 2020-10-22
  • 1970-01-01
  • 2015-09-28
  • 2014-04-16
相关资源
最近更新 更多