Skip to content Skip to sidebar Skip to footer

How To Get Document Id Of The Postion Of Recycler View Item?

I was using firebaseUI library to populate recycler view using firestore database. when I try to retrieve the document id when i click on recycler view item it was like this Docum

Solution 1:

The method you are trying to get uid of item i.e:

DocumentSnapshotsnapshot= getSnapshots().getSnapshot(holder.getAdapterPosition());
finalStringcountryName= snapshot.getId();

will work only in case of FirestoreRecyclerAdapter. As you are using a RecyclerView.Adapter you have to add uid in your Model class and then Retrieve it.

CountryItem.class

publicclassCountryItem{
    privateString CountryName;
    privateString uid;
   //add getters and setters
}

And while Retrieving the data from firestore add uid field to your object. I assume you are using query for this then it would be like:-

Queryquery= firestore.collection("Collection").orderBy("CountryName",Query.Direction.ASCENDING);

query.addSnapshotListener(this, newEventListener<QuerySnapshot>() {
    @OverridepublicvoidonEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
        for(DocumentSnapshot documentSnapshot: documentSnapshots) {
            CountryItemcountryItem= documentSnapshot.toObject(CountryItem.class);

             countryItem.setuid(documentSnapshot.getId().toString());
             countryItemList.add(countryItem);
        }
    }

});

And onClick of an item you can get uid and pass to your method:-

holder.view.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {

        String uid = categorylist.get(position).getUid();

     }
});

Post a Comment for "How To Get Document Id Of The Postion Of Recycler View Item?"