I have 2 different collections in Cloud Firestore:
- accounts (identifier: userId)
- username
- pictureURL
- posts (identifier: id)
- userId
- text
- likes
Aim: I want to have these two collections in Fragment with a Androidx RecyclerView to show the posts with the username and pictureURL.
I used "androidx.recyclerview:recyclerview:1.1.0" in my build.gradle.
I use the following method for getting the Posts from Firebase and store them in the model "FirebasePost":
/**
* Get all FirebasePost objects from Firebase Firestore collection
* @return
*/
public FirestoreRecyclerOptions<FirebasePost> getPosts() {
Query mQuery = mFirestore.collection("posts").orderBy("createdAt", Query.Direction.DESCENDING);
// Get the response elements
FirestoreRecyclerOptions<FirebasePost> response = new FirestoreRecyclerOptions.Builder<FirebasePost>()
.setQuery(mQuery, FirebasePost.class)
.build();
return response;
}
I populate the RecyclerView by the following code:
/**
* Prepares the Recycler View for showing the Posts data
* @param mView Gets the current view
*/
private void prepareRecyclerView(View mView, FirestoreRecyclerOptions<FirebasePost> response) {
adapter = new FirestoreRecyclerAdapter<FirebasePost, PostHolder>(response) {
@Override
public void onBindViewHolder(@NonNull PostHolder holder, int position, @NonNull FirebasePost model) {
// Bind the FirebasePost object to the PostHolder
holder.setPostText_to_UI(model.getText());
holder.setTimestamp_to_UI(model.getCreatedAt());
holder.setAuthorDisplayName_to_UI(model.getAuthorUserId());
}
@NonNull
@Override
public PostHolder onCreateViewHolder(@NonNull ViewGroup group, int i) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.posts_item_cardview, group, false);
return new PostHolder(view);
}
};
mPostsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mPostsRecyclerView.setItemAnimator(new DefaultItemAnimator());
mPostsRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
How can I implement a second request to Firebase to get the user information and add these into the right places of the RecyclerView?
The problem is, that I get the AuthorUserId but not the displayName of the User because I cannot perform another request to Firestore, before giving the model to the RecyclerView
.