Search code examples
javaandroidfirebasegoogle-cloud-firestorefirebaseui

FirebaseUI - Populate RecyclerView on Dynamic Query Data


I have a Collection in firebase that has a certain number of items, let's call it Collection "FOLLOWING". This number of items will constantly be changing. At any given time, I want to listen for the number of items in this Collection and create several whereEqualto() calls on a Query object that is based on another 'Collection', let's call it "POLLS":

        Query followingQuery = mStoreBaseRef.collection(USERS_LABEL).document(id).collection(FOLLOWING_LABEL);
        x = followingQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            //This number is constantly changing, and I want to use it to query the "Polls" node
            int numberOfUsersFollowed = documentSnapshots.size();
            if (documentSnapshots.isEmpty() || documentSnapshots == null) {
                mRecyclerview.setVisibility(View.INVISIBLE);
                return;
            } else {
                Log.v("NUBER_OF_USER_FOLLOWED", String.valueOf(numberOfUsersFollowed));
                mFollowingUserQuery = mStoreBaseRef.collection(POLLS_LABEL);
                //Based on the number of users being "Followed," I want to search for those users' respective polls in the "Polls" node, and populate the RecyclerView based on this information
                for (DocumentSnapshot x : documentSnapshots) {
                    Following followedUser = x.toObject(Following.class);
                    String userID = followedUser.getUser_id();
                    Log.v("FOLLOWED_USER_ID", userID);
                    mFollowingUserQuery = mFollowingUserQuery.whereEqualTo(USER_ID_LABEL, userID);
                }

                FirestoreRecyclerOptions<Poll> storeOptions = new FirestoreRecyclerOptions.Builder<Poll>()
                        .setQuery(mFollowingUserQuery, Poll.class)
                        .build();

                mFirestoreAdaper = new FirestoreRecyclerAdapter<Poll, PollHolder>(storeOptions) {
                    @Override
                    protected void onBindViewHolder(@NonNull final PollHolder holder, final int position, @NonNull Poll model) {
                        holder.mPollQuestion.setText(model.getQuestion());
                        String voteCount = String.valueOf(model.getVote_count());
                        //TODO: Investigate formatting of vote count for thousands
                        holder.mVoteCount.setText(voteCount);
                        Picasso.with(getActivity().getApplicationContext())
                                .load(model.getImage_URL())
                                .fit()
                                .into(holder.mPollImage);
                        holder.mView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent toClickedPoll = new Intent(getActivity(), PollHostActivity.class);
                                String recyclerPosition = getSnapshots().getSnapshot(position).getId();
                                Log.v("Firestore ID", recyclerPosition);
                                toClickedPoll.putExtra("POLL_ID", recyclerPosition);
                                startActivity(toClickedPoll);

                            }
                        });
                    }

                    @Override
                    public PollHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                        View v = LayoutInflater.from(parent.getContext())
                                .inflate(R.layout.latest_item, parent, false);
                        return new PollHolder(v);
                    }
                };

                mRecyclerview.setAdapter(mFirestoreAdaper);
                scrollToPosition();
                mFirestoreAdaper.startListening();

            }
        }
    });

Essentially, the number of whereEqualTo() will be dynamic.

EDIT: My FirebaseUI RecylcerView is not populating any of the data based on the query above. I have all of these methods in my .onStart(), so I would expect that based on the changes in the "Following" node it would dynamically populate, however it is blank.


Solution

  • It looks like you're expecting multiple where clauses to act as logical OR, with the expectation that you'll get documents that match any of the conditions you're specifying with whereEqualTo. That's not how Firestore queries work. When you have multiple conditions, they act as logical AND, meaning all of the conditions must be true in order for a document to match the query.

    If you want a logical OR, you will have to perform multiple queries for each of the conditions, then merge the results together. This means you won't be able to use FirestoreRecyclerAdapter, as it requires a single query.