Following this I am trying to fill my RecyclerView
with posts from my Firestore.
My Firestore is structured exactly the same as in the linked answer so I am trying to replicate it. I think my issue is that I am not accessing the name of the documents properly. Would I have to use a .get()
to retrieve the name? Below is what I have:
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);
}
}
});
It should out put posts from all of the users that the current user is following but my output is blank
Edit:
Here is a screenshot of my DB:
The sub collection
UserIsFollowing
contains documents named after the users that the user is following whose only field is a boolean Set to true.
Edit 2:
I changed the code so that I first retrieve the list of followers then use that list to get their posts but this is still not working:
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);
}
}
});
}
}
}
});
Edit 3:
I am getting this error:
E/RecyclerView: No adapter attached; skipping layout
No adapter attached; skipping layout