【问题标题】:Querying a list of objects from Firebase Firestore on Android在 Android 上从 Firebase Firestore 查询对象列表
【发布时间】:2018-08-22 10:35:48
【问题描述】:

我正在尝试创建一种方法来从我的Cloud Firestore database 中查询项目列表。然后我会用所述列表设置一个RecyclerView 适配器,它应该可以很好地显示。截至目前,RecyclerView 为空。 (我还用虚拟项目测试了RecyclerView,它们显示得很好)

在我的方法结束时,项目列表仍然为空。日志以奇怪的顺序显示(最后一个日志显示在其他日志之前)。日志顺序让我怀疑侦听器中运行了一些单独的线程。我不确定如何同步它们。

private List<Project> projects;
private FirebaseFirestore db;

...在 onCreateView() 中(我正在片段中工作):

 db = FirebaseFirestore.getInstance();
 queryAllProjects();
 recyclerView.setAdapter(new ProjectRecyclerViewAdapter(projects, ...

...

 private void queryAllProjects() {
        projects = new ArrayList<>(); 
        //I've already tried to make a local list and return that, however, 
        //the compiler would force me to declare the list as final here, and it wouldn't solve anything.
        db.collection("projects")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d(TAG, document.getId() + " => " + document.getData());
                                Project project = document.toObject(Project.class);
                                projects.add(project);
                            }
                            Log.d(TAG, "After for loop: " + projects.toString()); 
                            //Here the list is OK. Filled with projects. 
                            //I'd like to save the state of the list from here
                        } else {
                            Log.d(TAG, "Error getting document: ", task.getException());
                            Toast.makeText(getContext(), R.string.error, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
        Log.d(TAG, "End of method: " + projects.toString()); 
        //Oddly enough, this log displays before the other logs. 
        //Also, the list is empty here, which is probably what I'm unintentionally feeding into the Recycler View's adapter
    }

这是我一直关注的官方文档

https://firebase.google.com/docs/firestore/query-data/get-data#custom_objects

【问题讨论】:

  • 添加到适配器后是否调用了notifyDataSetChange
  • 好吧,在这种情况下,项目列表在设置适配器之前更改,而不是之后。所以应该没有必要通知它。 (刚刚用 onCreateView 中的内容更新了帖子)。或者,我尝试设置一个带有空列表的适配器,然后调用我的查询方法,然后通知。但结果相同。

标签: java android firebase google-cloud-firestore


【解决方案1】:

您现在不能使用尚未加载的东西。换句话说,您不能简单地将projects ArrayList 作为全局变量并在onComplete() 方法之外使用它的值,因为由于该方法的异步行为,它将始终为null。这意味着当您尝试使用以下日志语句时:

Log.d(TAG, "End of method: " + projects.toString()); 

数据尚未从数据库加载完毕,因此无法访问。

解决这个问题的一个快速方法是将这行代码移到 onComplete() 方法中,否则我建议你从这个 post 中查看我的答案的最后一部分,其中我已经解释了如何使用自定义回调来完成。您也可以看看这个 video 以获得更好的理解。

还有另一个 answer 展示了如何在回调之外获取您的列表。

【讨论】:

    【解决方案2】:
    projects.add(project);
    adapter.notifyDataSetChange()
    

    添加列表后调用 notifyDataSetChange()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      相关资源
      最近更新 更多