【问题标题】:How to get data from Firebase Firestore?如何从 Firebase Firestore 获取数据?
【发布时间】:2020-02-24 10:59:44
【问题描述】:

在这段代码中,我在 Firestore 中添加了“评论”,这不是问题。

btnAddComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnAddComment.setVisibility(View.INVISIBLE);
                DocumentReference comment = firestore.collection("Comment").document(postKey);
                String comment_content = editTextComment.getText().toString();
                String uid = currentUser.getUid();
                String uname = currentUser.getDisplayName();
                String uimg = currentUser.getPhotoUrl().toString();
                Comment comm = new Comment(comment_content, uid, uimg, uname);

                comment.set(comm).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        showMessage("Добавлено");
                        editTextComment.setText("");
                        btnAddComment.setVisibility(View.VISIBLE);
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        showMessage("Не добавлено: " + e.getMessage());
                    }
                });
            }
        });

下面是如何缝合它:

private void iniRvComment() {
        RwComment.setLayoutManager(new LinearLayoutManager(this));
        DocumentReference docRef = firestore.collection("Comment").document(postKey);
        docRef.collection("Comment").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                if (documentSnapshot != null && !documentSnapshot.getDocuments().isEmpty()) {
                    listComment = new ArrayList<>();
                    List<DocumentSnapshot> documents = documentSnapshot.getDocuments();
                    for (DocumentSnapshot value : documents) {

                        Comment comment = value.toObject(Comment.class);
                        listComment.add(comment);
                    }
                    commentAdapter = new CommentAdapter(getApplicationContext(), listComment);
                    RwComment.setAdapter(commentAdapter);
                }
            }
        });
    }

我想使用“postKey”作为键显示带有 Firestore 的“评论”字段,但我可以理解这段代码的工作原理,在 Firebase 官方网站上观看了所有内容。我在用户发表评论的每个帖子下都有一个“帖子”,问题是没有显示某些内容。

【问题讨论】:

  • 你能分享它给出的错误吗?

标签: java android firebase google-cloud-firestore


【解决方案1】:

由于您的数据库引用声明,上述代码将无法运行。如果您想检索所有 cmets,只需提供集合参考。

firestore.collection("Comment").addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
        if (documentSnapshot != null && !documentSnapshot.getDocuments().isEmpty()) {
            listComment = new ArrayList<>();
            List<DocumentSnapshot> documents = documentSnapshot.getDocuments();
            for (DocumentSnapshot value : documents) {

                Comment comment = value.toObject(Comment.class);
                listComment.add(comment);
            }
            commentAdapter = new CommentAdapter(getApplicationContext(), listComment);
            RwComment.setAdapter(commentAdapter);
        }
    }
});

【讨论】:

    【解决方案2】:

    正如我在您的屏幕截图中看到的,您的架构如下所示:

    Firestore-root
        |
        --- Comment (collection)
              |
              --- docId
                   |
                   --- content: "hfjnncjf"
    

    如果要获取所有Comment对象,只需要使用下面的CollectionReference

    CollectionReference commentRef = firestore.collection("Comment");
    commentRef.addSnapshotListener(/* ... */);
    

    您的代码中的错误是在您的 commentRef 对象上添加了 .collection("Comment") 调用。您在做什么,您假设您的 commentRef 文档中有一个子集合,这是错误的。你那里没有这样的子集合。

    如果你想获得一条评论,那么你应该使用以下代码行:

    DocumentReference commentRef = firestore.collection("Comment").document(postKey);
    

    但没有迭代,因为你只得到一个文档。

    编辑:

    要获取content 属性的值并实时监听变化,请使用以下代码行:

    FirebaseFirestore firestore = FirebaseFirestore.getInstance();
    DocumentReference commentRef = firestore.collection("Comment").document("ZQMqIrx19fftjO3ZeSzkuEw87MY2");
    commentRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirebaseFirestoreException e) {
            if (e != null) {
                Log.w(TAG, "Listen failed.", e);
                return;
            }
    
            if (snapshot != null && snapshot.exists()) {
                Log.d(TAG, snapshot.getString("content"));
            }
        }
    });
    

    您的 logcat 中的输出将是:

    hfjnncjf
    

    【讨论】:

    • 我在每个帖子下都有(帖子)留下一个(Сomment)我使用“String postKey”作为键。我需要在每个“帖子”下获得一个“评论”
    • 发布(ZQMqIrx19fftjO3ZeSzkuEw87MY2) |评论(postKey(ZQMqIrx19fftjO3ZeSzkuEw87MY2))
    • Firestore-root | --- 收藏(“评论”)| --- 文档(postKey() | --- 内容:“hfjnncjf”
    • 那么您是否尝试仅使用我的答案中的参考?是这样的吗?
    • 帖子下方有“帖子”,用户离开“评论”程序作品;一切都保存了,但是有一个问题,我从官方网站上拿的每个“帖子”编写的代码下都放不下所有这些“评论”我不明白为什么它不起作用
    猜你喜欢
    • 2018-11-06
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 2022-09-23
    相关资源
    最近更新 更多