Reputation: 122
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;
}
}
Upvotes: 2
Views: 2344
Reputation: 3001
The method you are trying to get uid of item i.e:
DocumentSnapshot snapshot = getSnapshots().getSnapshot(holder.getAdapterPosition());
final String countryName = 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
public class CountryItem {
private String CountryName;
private String 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:-
Query query = firestore.collection("Collection").orderBy("CountryName",Query.Direction.ASCENDING);
query.addSnapshotListener(this, new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
for(DocumentSnapshot documentSnapshot: documentSnapshots) {
CountryItem countryItem = 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(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uid = categorylist.get(position).getUid();
}
});
Upvotes: 3