This question grew out of this. I am looking for an adapter to populate my RecyclerView
with multiple queries.
I am currently using the FirestoreRecyclerAdapter
but was informed that since it only accepts a single query I cannot use it to populate a RecyclerView
with selected documents in a collection.
What I currently have:
Query posts = db.collection("posts");
FirestoreRecyclerOptions<Post> options = new FirestoreRecyclerOptions.Builder<Post>()
.setQuery(posts, Post.class)
.build();
adapter = new FirestoreRecyclerAdapter<Post, PostViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull PostViewHolder postViewHolder, int position, @NonNull Post post) {
postViewHolder.setPost(post);
}
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_layout, parent, false);
return new PostViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
This currently retrieves all posts in my posts collection however I want to use a list to filter for only posts that contain userId's in said list. Thank you for any help.
As Doug Stevenson mentioned in his answer:
You will have to make a query for each one of the documents you want to display, collect the results in memory, and use a different type of adapter to populate the view.
As I see in your code, you are still using a single query, which in your code is named posts
. For example, if you want to get documents from within two queries, two query objects are required. In code, this is how you can merge two queries locally:
As you can see, the onSuccess()
method has as an argument, a List<Object>
, which basically contains the result of both queries.
I am looking for an adapter to populate my recycler view with multiple queries.
And to answer your question, since you want to display the data in a RecyclerView
, the most appropriate type of adapter that might be is a RecyclerView.Adapter
and not FirestoreRecyclerAdapter
which doesn't accept a list. Pass that list to the adapter's constructor and display each element in its own ViewHolder
. That's it!