Search code examples
javaandroidfirebasegoogle-cloud-firestorefirebaseui

Cannot Access Recyclerview Position Using FirbaseUI with Cloud Firestore


I am migrating my application from the Firebase Database to the Firebase Cloud Firestore.

Previously, I was using the FirebaseUI for the realtime database. After initializing all of my options and creating the adapter in the realtime database, I called .setOnClickListener() on a View of the RecyclerView to navigate to a new activity:

holder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent toClickedPoll = new Intent(getActivity(), PollHostActivity.class);
                        toClickedPoll.putExtra("POLL_ID", mFireAdapter.getRef(holder.getAdapterPosition()).getKey());
                        startActivity(toClickedPoll);

                    }
                });

The extra I passed was the key of the location in my Firebase realtime database.

I am trying to access the same "key" via the FirebaseUI for the Cloud Firestore. Below is my code:

  mFirestoreAdaper = new FirestoreRecyclerAdapter<Poll, PollHolder>(storeOptions) {
            @Override
            protected void onBindViewHolder(@NonNull final PollHolder holder, 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 position = String.valueOf(mFirestoreAdaper.getItemId(holder.getAdapterPosition()));
                        Log.v("Firestore ID", position);
                        toClickedPoll.putExtra("POLL_ID", position);
                        startActivity(toClickedPoll);

                    }
                });
            }

Right now, the position variable (I have logged) is returning -1. I essentially want it to return the Poll document below:

enter image description here


Solution

  • Try this:

       String positions = getSnapshots().getSnapshot(position).getId();
       Log.v("Firestore ID", positions);
       toClickedPoll.putExtra("POLL_ID", positions);
    

    instead of this:

        String position = String.valueOf(mFirestoreAdaper.getItemId(holder.getAdapterPosition()));
                        Log.v("Firestore ID", position);
                        toClickedPoll.putExtra("POLL_ID", position);