gpt4 book ai didi

java - 如何从 Firebase Firestore 获取数据?

转载 作者:行者123 更新时间:2023-12-01 18:15:42 25 4
gpt4 key购买 nike

在此代码中,我在 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 官方网站上观看了这一切。我在用户留下评论的每个帖子下都有一个“帖子”,问题是没有显示某些内容。

最佳答案

由于您的数据库引用声明,上述代码将不起作用。如果您想检索所有评论,只需提供集合引用即可。

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);
}
}
});

关于java - 如何从 Firebase Firestore 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60374474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com