2

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);

FireStore DataBase

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
David
  • 769
  • 1
  • 6
  • 28

1 Answers1

2

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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 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. – David Jul 03 '19 at 17:48
  • Also why can't FirestoreRecyclerAdapter accept a list of queries if it extends RecyclerView.Adapter? – David Jul 03 '19 at 19:39
  • You should know since you get the number from the database. If you have more than two queries, you can use a `List>` as in my answer from this **[post](https://stackoverflow.com/questions/51892766/android-firestore-convert-array-of-document-references-to-listpojo/51897261)**. So you can pass a list of tasks to the `whenAllSuccess()`. There can be two (as in my example), there or as many as you want. – Alex Mamo Jul 03 '19 at 22:34
  • 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](https://github.com/firebase/FirebaseUI-Android/blob/master/firestore/src/main/java/com/firebase/ui/firestore/FirestoreRecyclerAdapter.java) constructor because none of its constructors allow you to lass a list. You are allowed only to pass a `FirestoreRecyclerOptions` object, right? – Alex Mamo Jul 03 '19 at 22:37
  • I am facing a similar problem, if you would help me I would be very grateful https://stackoverflow.com/questions/68006079/java-android-use-listadapter-to-show-posts-of-followed-users – Conta Jun 17 '21 at 10:19
  • @pophome I'll take a look and if I'll know the answer, I'll write to you. – Alex Mamo Jun 17 '21 at 10:30