【发布时间】: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