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!
What if I don't know how many queries I will need to make? The list I want to make queries of is not of a set length.
Also why can't FirestoreRecyclerAdapter accept a list of queries if it extends RecyclerView.Adapter?
You should know since you get the number from the database. If you have more than two queries, you can use a
List<Task<DocumentSnapshot>>
as in my answer from this post. So you can pass a list of tasks to thewhenAllSuccess()
. There can be two (as in my example), there or as many as you want.First of all you aren't passing to the
RecyclerView.Adapter
a list of queries, you are passing a list of objects that is coming from the database. You cannot pass any list to the FirestoreRecyclerAdapter constructor because none of its constructors allow you to lass a list. You are allowed only to pass aFirestoreRecyclerOptions
object, right?