Java 如何在云Firestore中使用Firebase正确重写代码?
我写信连接到Firebase,现在我想把一切都转移到云Firestore 1) 编写第一个方法是为了从firebase获取“注释”Java 如何在云Firestore中使用Firebase正确重写代码?,java,firebase,android-studio,google-cloud-firestore,Java,Firebase,Android Studio,Google Cloud Firestore,我写信连接到Firebase,现在我想把一切都转移到云Firestore 1) 编写第一个方法是为了从firebase获取“注释” private void iniRvComment() { RvComment.setLayoutManager(new LinearLayoutManager(this)); DatabaseReference commentRef = firebaseDatabase.getReference(COMMENT_KEY).child
private void iniRvComment() {
RvComment.setLayoutManager(new LinearLayoutManager(this));
DatabaseReference commentRef = firebaseDatabase.getReference(COMMENT_KEY).child(postKey);
commentRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
listComment = new ArrayList<>();
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
Comment comment = snapshot.getValue(Comment.class);
listComment.add(comment);
}
commentAdapter = new CommentAdapter(getApplicationContext(), listComment);
RvComment.setAdapter(commentAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void iniRvComment(){
RvComment.setLayoutManager(新的LinearLayoutManager(本));
DatabaseReference commentRef=firebaseDatabase.getReference(COMMENT_KEY).child(postKey);
commentRef.addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
listComment=新的ArrayList();
对于(DataSnapshot快照:DataSnapshot.getChildren()){
Comment Comment=snapshot.getValue(Comment.class);
添加(注释);
}
commentAdapter=新的commentAdapter(getApplicationContext(),listComment);
RvComment.setAdapter(commentAdapter);
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});
}
2) 以及如何重写此代码以从CloudFireStore获取“注释”。我在下面写的不正确
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);
}
}
});
}
private void iniRvComment(){
RwComment.setLayoutManager(新的LinearLayoutManager(本));
DocumentReference docRef=firestore.collection(“Comment”).document(postKey);
docRef.collection(“Comment”).addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(@Nullable QuerySnapshot documentSnapshot,@Nullable FirebaseFirestoreException e){
if(documentSnapshot!=null&&!documentSnapshot.getDocuments().isEmpty()){
listComment=新的ArrayList();
List documents=documentSnapshot.getDocuments();
for(文档快照值:文档){
Comment Comment=value.toObject(Comment.class);
添加(注释);
}
commentAdapter=新的commentAdapter(getApplicationContext(),listComment);
rwcoment.setAdapter(commentAdapter);
}
}
});
}
我建议将您的评论平铺成一个单一的顶级
评论集,而不是将其存储在帖子下。这将允许您执行许多有用的搜索操作,例如按用户或帖子搜索所有评论,甚至创建“最近活动的帖子”提要
要实现这一点,您需要更改数据库结构,以便将所有评论与它们附加到的帖子一起存储
{
content: "...",
timestamp: 1234534568425,
postId: "...",
uid: "...",
uimg: "...",
uname: "..."
}
完成此操作后,您现在可以使用以下方式通过post查询评论:
private final int PAGE_SIZE=10;
private void inervcomment(){
RvComment.setLayoutManager(新的LinearLayoutManager(本));
firestore.collection(“Comments”)//这是一个顶级集合
.whereEqualTo(“posted”,postKey)//为给定的帖子选择评论
.orderBy(“timestamp”,Query.Direction.DESCENDING)//从最新到最旧的顺序
.limit(PAGE\u SIZE)//获取最新的评论
.addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(@Nullable QuerySnapshot results,
@可为空的FireBaseFireStore异常(e){
如果(e!=null){
Log.w(标记“侦听失败”,e);
返回;
}
listComment=新的ArrayList(页面大小);
对于(QueryDocumentSnapshot commentDoc:results){
Comment commentObj=commentDoc.toObject(Comment.class);
添加(commentObj);
}
commentAdapter=新的commentAdapter(getApplicationContext(),listComment);
RvComment.setAdapter(commentAdapter);
Log.d(标记,“检索的”+结果.size()+“最近的评论”);
}
});
}
请发布数据库结构添加屏幕截图。共享代码有什么问题?无法获取“评论”@ArmanAbdullin抱歉,这是Javascript语法。更新为Java语法。不输出页面大小
可以是您想要的大小。如果你只显示10条评论,那么下载500条评论是没有意义的。此外,此代码(在显示数据库之前发布)当前与您存储评论的方式不兼容,因为您没有在评论中存储posted
。一旦你这样做了,这段代码就应该可以工作了。你似乎也有一个错误,你没有正确地存储时间戳。