我当时使用firebaseUI
库通过firestore
数据库填充回收者视图。
当我尝试检索文档ID 时,我单击“回收站”视图项就是这样
DocumentSnapshot snapshot = getSnapshots()
.getSnapshot(holder.getAdapterPosition());
final String countryName = snapshot.getId();
我尝试了这段代码,但是getSnapshots()
出现在onbindeviewolder
内的红色单词和nt workng上,所以我尝试了一下,但也没有起作用
DocumentReference aa=firebaseFirestore.collection("MainCategories").document(String.valueOf(SS));
String aaa=aa.getId();
由于上面的代码在这里不起作用
,因此有什么方法可以检索文档ID。 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();
}
}
持有人类别
私有类AllCategoriesViewHolder扩展了RecyclerView.ViewHolder { 私人视图视图;
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);
}
}
和模型类
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;
}
}
答案 0 :(得分:0)
您试图获取项uid的方法,即:
DocumentSnapshot snapshot = getSnapshots().getSnapshot(holder.getAdapterPosition());
final String countryName = snapshot.getId();
仅在FirestoreRecyclerAdapter
时有效。在使用RecyclerView.Adapter
时,您必须在Model类中添加uid,然后对其进行检索。
CountryItem.class
public class CountryItem {
private String CountryName;
private String uid;
//add getters and setters
}
在从firestore检索数据时,将uid字段添加到您的对象。我假设您正在为此使用查询,那么它将是:-
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);
}
}
});
onClick
的项目可以获取uid并传递给您的方法:-
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uid = categorylist.get(position).getUid();
}
});