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.
New document with same ID should not be allowed in the same collection. It should be possible to fetch an already existing document from a previous import.
The documentation says that the limit for all document IDs is 1500 bytes.
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.
In firebaseui 5.0.0 , "getSnapshots()" is not present. In onBindViewHolder you have to use..
getItem(position).getId()
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 )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With