【问题标题】:How do i List out Firebase Document wihtin a Collection?如何列出集合中的 Firebase 文档?
【发布时间】:2020-04-01 13:13:49
【问题描述】:

我有一个名为“测验”的集合,它的文档包含Quiz 类别。如何获取所有类别文件?只喜欢(科学,技术..等..)

View Image

我已尝试使用此代码:

 db.collection("Quiz")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Log.d("KKKK : ", document.getId() + " => " + document.getData());
                        }
                    } else {
                        Log.d("KKKK : ", "Error getting documents: ", task.getException());
                    }
                }
            });

但它永远不会返回值我已将 Firebase 规则更改为 allow read, write: if true;

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    如果db对象定义如下:

    FirebaseFirestore db = FirebaseFirestore.getInstance();
    

    要获取Questions 子集合中存在的所有文档,请使用以下参考:

    db.collection("Quiz").document("Science")
        .collection("Questions")
        .get()
        .addOnCompleteListener(/* ... */);
    

    看,您需要在参考中添加所有集合和文档名称,而不仅仅是一个,因为它现在在您的实际代码中。

    【讨论】:

    • 嗨...但我需要列出第一个类别,例如(科学、技术...等...)
    • 在这种情况下,请使用您已有的代码并尝试add the results to a list。然后根据用户选择的内容,将所选类别添加到您的参考资料中,就像我的回答中的参考资料一样。
    猜你喜欢
    • 2022-01-02
    • 2018-10-21
    • 2021-04-24
    • 2020-08-28
    • 1970-01-01
    • 2018-03-17
    • 2020-03-19
    相关资源
    最近更新 更多