问题
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());
}
});
}
});
And here's how to stitch it:
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);
}
}
});
}
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.
回答1:
The above code won't work because of your database reference declaration. If you want to retrieve all the comments just provide collection referece.
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:
As I see in your screenshot, you have a schema that looks like this:
Firestore-root
|
--- Comment (collection)
|
--- docId
|
--- content: "hfjnncjf"
If you want to get all Comment objects, you only need to use the following CollectionReference:
CollectionReference commentRef = firestore.collection("Comment");
commentRef.addSnapshotListener(/* ... */);
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.
Edit:
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"));
}
}
});
The output in your logcat will be:
hfjnncjf
来源:https://stackoverflow.com/questions/60374474/how-to-get-data-from-firebase-firestore