Sharon
Sharon

Reputation: 3909

How do I get the Firestore document ID when using FirestoreRecyclerAdapter?

I'm using Firestore to store a collection of 'charts'. I need to query the collection to get all charts with the logged-in user's ID. I have a FirebaseRecyclerAdapter which shows the appropriate charts (the query is passed in via FirestoreRecyclerOptions).

However, I need to access the document key for each chart, and I can't figure out how to get that information. Ideally I'd like to set it as a variable in the Chart model as soon as the chart is retrieved from the database, but the 'map' doesn't seem to provide a way to map the document key to a value in the model. If it's not possible to do that, then another solution would be something like retrieving it in BindViewHolder and storing it in an invisible view to be read later.

In my main activity I have:

FirestoreRecyclerOptions<Chart> recyclerOptions = new FirestoreRecyclerOptions.Builder<Chart>()
                .setQuery(mChartsQuery, Chart.class)
                .build();

        mAdapter = new ChartListAdapter(recyclerOptions, this.getActivity());

where mChartsQuery is getting the charts with a specific user id.

A Chart is created like this (the user has entered 'nameString' into a form field):

   Map<String, Object> newChart = new HashMap<>();
    newChart.put("uid", userId);
    newChart.put("name", nameString);

    mDatabase.collection("charts")
            .add(newChart)
            .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {...});

My ChartListAdapter class is:

public class ChartListAdapter
        extends FirestoreRecyclerAdapter<Chart, ChartViewHolder> {

    public ChartListAdapter(FirestoreRecyclerOptions recyclerOptions) {
        super(recyclerOptions);

    }

    @Override
    protected void onBindViewHolder(ChartViewHolder holder, int position, Chart model) {

        // THIS IS WHERE I NEED THE DOCUMENT ID, SO I CAN
        // PASS IT INTO THE INTENT AS EXTRA_CHART_KEY 

        // Set click listener for the chart
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ChartViewActivity.class);
                intent.putExtra(ChartViewActivity.EXTRA_CHART_KEY, chartKey);
                startActivity(intent);
            }
        });
        // Bind Chart to ViewHolder
        holder.bindToChart(model);
    }

    @Override
    public ChartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_chart, parent, false);

        return new ChartViewHolder(view);
    }
}

So, I need to be able to retrieve the Document ID in onBindViewHolder. I found a solution which said to use getSnapshots().get(position).getKey(); but getSnapshots().get(position) just retrieves a Chart, which doesn't have the key set up in it.

Upvotes: 7

Views: 2153

Answers (4)

Zephania Mwando
Zephania Mwando

Reputation: 118

It should be this line which you can actually log and check from your logcat

getSnapshots().getSnapshot(position).getId();
Log.i("chartKey==", String.valueOf(getSnapshots().getSnapshot(position).getId();))

Then you can cast it to a string if need be this way;

String chartKey= getSnapshots().getSnapshot(position).getId();
Log.i("chartKey==",chartKey )

Upvotes: 1

trevorrrrr
trevorrrrr

Reputation: 1

I'm using com.firebaseui:firebase-ui-firestore:6.2.1, but seems getSnapshots() can still access, so just using these in onBindViewHolder

snapshots.getSnapshot(position).id

which will look like this:

// Kotlin

override fun onBindViewHolder(holder: MyHolder, position: Int) {
    // ...

    holder.itemView.setOnClickListener {_ ->
            // ...

            snapshots.getSnapshot(position).id
    }
}

Logcat output:

2020-05-19 23:10:58.595 14607-14607/com.app I/TESTTT: EFaJlMVKCcOLB1PvtQVg => {}
2020-05-19 23:10:58.598 14607-14607/com.app I/TESTTT: XdnrhZiURgqwk2vvoJS9 => {}
2020-05-19 23:10:58.600 14607-14607/com.app I/TESTTT: sCPpsMVfYw285tFZ4neS => {}
2020-05-19 23:10:58.602 14607-14607/com.app I/TESTTT: AoQjstzjIKlFpvDxNxWS => {}
2020-05-19 23:11:04.745 14607-14607/com.app I/TESTTT: onClickEvent Doc ID: EFaJlMVKCcOLB1PvtQVg
2020-05-19 23:11:13.963 14607-14607/com.app I/TESTTT: onClickEvent Doc ID: XdnrhZiURgqwk2vvoJS9
2020-05-19 23:11:17.061 14607-14607/com.app I/TESTTT: onClickEvent Doc ID: AoQjstzjIKlFpvDxNxWS
2020-05-19 23:11:19.245 14607-14607/com.app I/TESTTT: onClickEvent Doc ID: sCPpsMVfYw285tFZ4neS

Upvotes: 0

Shuvendu Dhal
Shuvendu Dhal

Reputation: 85

In firebaseui 5.0.0 , "getSnapshots()" is not present. In onBindViewHolder you have to use..

getItem(position).getId()

Upvotes: 1

Sharon
Sharon

Reputation: 3909

Found the solution... In onBindViewHolder I was using

getSnapshots().get(position).getKey();

but it should have been

getSnapshots().getSnapshot(position).getId();

The correct version works fine, and gets the document ID.

Upvotes: 11

Related Questions