I'm trying to show all posts from users followed and I'm using a Recyclerview:
FirestoreRecyclerOptions<GetInfoPost> options
= new FirestoreRecyclerOptions.Builder<GetInfoPost>()
.setQuery(FirebaseFirestore.getInstance().collection("following").document(fAuth.getCurrentUser().getUid()).collection("/userFollowing").limit(1000), GetInfoPost.class)
.build();
FirestoreRecyclerAdapter<GetInfoPost, FindFriendsViewHolder> firestoreRecyclerAdapter
=new FirestoreRecyclerAdapter<GetInfoPost, FindFriendsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull FindFriendsViewHolder findFriendsViewHolder, int i, @NonNull GetInfoPost getInfoPost) {
final String listUserId = getSnapshots().getSnapshot(i).getId();
Query query = FirebaseFirestore.getInstance()
.collection("post/" + listUserId + "/userPosts")
.limit(1000);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
DocumentSnapshot snapshot = getSnapshots().getSnapshot(1);
final String postId = snapshot.getId();
FirebaseFirestore.getInstance().collection("posts/" + listUserId + "/userPosts").document(postId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
findFriendsViewHolder.txtDescrizione.setText(task.getResult().getString("description"));
}
});
}
});
}
As you can see I'm trying to get the description field from the last post, the problem is that I don't know the id of the last post. The post id is nothing more than the name of a collection and I want to be able to pick it up.
In the code shown above I tried to get it like this:
Query query = FirebaseFirestore.getInstance()
.collection("post/" + listUserId + "/userPosts")
.limit(1);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
DocumentSnapshot snapshot = getSnapshots().getSnapshot(1);
final String postId = snapshot.getId();
but I get a null description because the query does not open the name of the collection, but goes to get followers (even if I'm not sure enough about this). Is there a way to get the name of the first collection that the query finds? for example this:
