如何从Firebase Firestore获取数据? [英] How to get data from Firebase Firestore?
问题描述
在此代码中,我在Firestore中添加了注释",这不是问题.
In this code I added "Comment" in firestore this is not problem.
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的"Comment"字段,但是我可以理解此代码的工作原理,并在Firebase官方网站上观看了所有内容.用户发表评论的每个帖子下都有一个帖子",问题是没有显示任何内容.
I want to display the "Comment" field with firestore using "postKey" as the key but I can understand how this code works, watched it all on the official Firebase website. I have a "Post" under each post the user leaves a comment, the problem is that something is not displayed.
推荐答案
正如我在您的屏幕快照中所看到的,您有一个如下所示的架构:
As I see in your screenshot, you have a schema that looks like this:
Firestore-root
|
--- Comment (collection)
|
--- docId
|
--- content: "hfjnncjf"
如果要获取所有Comment
对象,则只需使用以下CollectionReference
:
If you want to get all Comment
objects, you only need to use the following CollectionReference
:
CollectionReference commentRef = firestore.collection("Comment");
commentRef.addSnapshotListener(/* ... */);
代码中的错误是在commentRef
对象上添加了.collection("Comment")
调用.您正在做什么,假设您在commentRef
文档中有一个子集合,这是错误的.您在那里没有这样的子集合.
What is wrong in your code is the addition of .collection("Comment")
call on your commentRef
object. What you are doing, you are assuming that you have a subcollection within your commentRef
document, which is wrong. You don't have such a subcollection there.
如果要发表评论,则应使用以下代码行:
If you want to get a single comment, then you should use the following line of code:
DocumentReference commentRef = firestore.collection("Comment").document(postKey);
但是无需重复,因为您只会得到一个文档.
But without iterating, because you only get one document.
要获取您的content
属性的值并实时监听更改,请使用以下代码行:
To get the value of your content
property and listen for changes in real-time, please use the following lines of code:
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中的输出将是:
The output in your logcat will be:
hfjnncjf
这篇关于如何从Firebase Firestore获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!