【发布时间】:2020-08-19 12:45:31
【问题描述】:
这就是我的 Firestore 数据库的样子:
我有一个显示帖子列表的 RecyclerView。但我无法检索包含子集合的文档并将其放入对象中。当我在RecyclerView 中提交帖子列表时,我希望每个帖子都已经有自己的点赞和评论。
我使用 AsyncTask 来做查询用户所有帖子的后台工作。 AsyncTask 完成后,它会将帖子列表提交到 RecyclerView。 (还是不行)
在下面的代码中,我调用了用户的所有帖子,然后获取每个帖子的“赞”。 我在每个帖子中都放了一个while循环,这样我就可以等待它完成对喜欢的查询,然后再继续下一个帖子快照
doInBackground()
final List<Post> postList = new ArrayList<>();
// -- GET all the posts of the user
feedsCollection.document(documentID).collection("Posts").get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for(final DocumentSnapshot documentSnapshot1 : queryDocumentSnapshots){
final Post post = documentSnapshot1.toObject(Post.class);
// Finish getting the likes&comments before proceeding
while(post.isStillLoading()){
// -- GET Likes of each post
db.collection("FeedsCollection").document(documentID).collection("Posts")
.document(documentSnapshot1.getId()).collection("Likes").get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
List<Like> likeList = new ArrayList<>();
for(DocumentSnapshot likeSnapshot : queryDocumentSnapshots){
Like like = likeSnapshot.toObject(Like.class);
likeList.add(like);
}
post.setLikes(likeList);
post.setStillLoading(false);
}
});
// -- GET comments code
}
postList.add(post);
}
}
});
return postList;
onPostExecute()
protected void onPostExecute(List<Post> postList) {
super.onPostExecute(postList);
adapter.submitData(postList);
}
这些是我尝试过的:
- 我尝试了多个 我的 recyclerview 中的视图类型,所以如果列表仍然是空的,它 显示“无项目”。然后当 AsyncTask 完成时,它会 向 recyclerview 重新提交列表。
- 我尝试使用 FirestoreRecyclerAdapter,但我了解到它的查询很浅,因此无法完全获取每个帖子文档的 Likes 和 Comments 子集合。
还有其他解决方法吗?
【问题讨论】:
标签: java android firebase android-recyclerview google-cloud-firestore