在this之后,我尝试用我的Firestore中的帖子填充RecyclerView
。
我的Firestore的结构与链接的答案完全相同,因此我尝试复制它。我认为我的问题是我没有正确访问文档的名称。我是否必须使用.get()
来检索名称?以下是我所拥有的:
private CollectionReference userFollowingReference = rootRef.collection("following/"+ currentUser.getUid()+"/UserIsFollowing");
Query first = rootRef.collection("posts/"+ userFollowingReference.getId()+"/userPosts")
.orderBy("date", Query.Direction.DESCENDING)
.limit(3);
first.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull final Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
Post post = document.toObject(Post.class);
list.add(post);
}
followerAdapter.notifyDataSetChanged();
RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
isScrolling = true;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
isScrolling = false;
Query nextQuery = userFollowingReference.startAfter(lastVisible).limit(3);
nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> t) {
if (t.isSuccessful()) {
for (DocumentSnapshot d : t.getResult()) {
Post post = d.toObject(Post.class);
list.add(post);
}
followerAdapter.notifyDataSetChanged();
lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);
if (t.getResult().size() < 3) {
isLastItemReached = true;
}
}
}
});
}
}
};
recyclerView.addOnScrollListener(onScrollListener);
}
}
});
应该从当前用户关注的所有用户那里发布帖子,但我的输出为空
编辑:
这是我的数据库的屏幕截图:
子集合UserIsFollowing
包含以用户关注的用户命名的文档,其唯一字段是将布尔值设置为true。
编辑2:
我更改了代码,以便首先检索关注者列表,然后使用该列表获取其帖子,但这仍然不起作用:
Query first = userFollowingReference;//TODO Realtime updates
first.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull final Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
String id = document.getId();
followingList.add(id);
}
followerAdapter.notifyDataSetChanged();
if(!followingList.isEmpty()) {
Query second = rootRef.collection("posts/" + followingList.get(0) + "/userPosts")
.orderBy("date", Query.Direction.ASCENDING)
.limit(3);
second.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull final Task<QuerySnapshot> task) {
if(task.isSuccessful()) {
List<Post> list = new ArrayList<>();
for(DocumentSnapshot document : task.getResult()){
Post post = document.toObject(Post.class);
list.add(post);
}
recyclerView.setAdapter(followerAdapter);
followerAdapter.notifyDataSetChanged();
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
isScrolling = true;
}
}
@Override
public void onScrolled(final RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
isScrolling = false;
Query nextQuery = rootRef.collection("posts")
.orderBy("date", Query.Direction.ASCENDING)
.startAfter(lastVisible).limit(3);
nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> t) {
if (t.isSuccessful()) {
for (DocumentSnapshot d : t.getResult()) {
Post post = d.toObject(Post.class);
postList.add(post);
}
followerAdapter.notifyDataSetChanged();
lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
if (t.getResult().size() < 3) {
isLastItemReached = true;
}
}
}
});
}
}
};
recyclerView.addOnScrollListener(onScrollListener);
}
}
});
}
}
}
});
修改3:
我收到此错误:
E/RecyclerView: No adapter attached; skipping layout
No adapter attached; skipping layout