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
DocumentSnapshot snapshot = getSnapshots()
.getSnapshot(holder.getAdapterPosition());
final String countryName = snapshot.getId();
I tred this code but getSnapshots()
appears by the red colored word and nt workng inside onbindeviewolder
so I tried this but not working too
DocumentReference aa=firebaseFirestore.collection("MainCategories").document(String.valueOf(SS));
String aaa=aa.getId();
So is there any method to retrieve the document id because the code above is not working here
private class CategoriesAdapter extends RecyclerView.Adapter<AllCategoriesViewHolder> {
private List<mainCategroies> categorylist;
CategoriesAdapter(List<mainCategroies> categorylist) {
this.categorylist = categorylist;
}
@NonNull
@Override
public AllCategoriesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_maincategory, parent, false);
return new AllCategoriesViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final AllCategoriesViewHolder holder, int position) {
final String categoryName = categorylist.get(position).getCategory_Name();
holder.setCategory_Name(categoryName);
String d = categorylist.get(position).getImageUrl();
holder.setImageUrl(d);
MainActivityProgress.setVisibility(View.GONE);
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return categorylist.size();
}
}
the holder class
private class AllCategoriesViewHolder extends RecyclerView.ViewHolder { private View view;
public AllCategoriesViewHolder(final View itemView) {
super(itemView);
view = itemView;
}
void setCategory_Name(String category_Name) {
TextView names = view.findViewById(R.id.CategoryName);
names.setText(category_Name);
}
void setImageUrl(String imageUrl) {
ImageView imageView = view.findViewById(R.id.MainCat_CardViewImage);
Picasso.get()
.load(imageUrl)
.fit()
.into(imageView);
}
}
and the model class
public class CountryItem {
private String CountryName;
public CountryItem(){
}
public CountryItem(String countryName) {
CountryName = countryName;
}
public String getCountryName() {
return CountryName;
}
public void setCountryName(String countryName) {
CountryName = countryName;
}
}
The method you are trying to get uid of item i.e:
will work only in case of
FirestoreRecyclerAdapter
. As you are using aRecyclerView.Adapter
you have to add uid in your Model class and then Retrieve it.CountryItem.class
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:-
And
onClick
of an item you can get uid and pass to your method:-