如何获取回收站视图项目位置的文档ID? [英] How to get document id of the postion of recycler view Item?

查看:74
本文介绍了如何获取回收站视图项目位置的文档ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 firebaseUI 库使用 firestore 数据库填充回收者视图.当我尝试检索文档ID 时,我单击回收站"视图项就是这样

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();

我尝试了此代码,但是 onbindeviewolder 中的红色单词和nt workng出现了 getSnapshots(),所以我尝试了一下但也没有起作用

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();

有什么方法可以检索文档ID,因为上面的代码在这里不起作用

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();
         }
         }

持有人类别

私有类AllCategoriesViewHolder扩展了RecyclerView.ViewHolder {私人视图;

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);
 }
 }

和模型类

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;
     }
}

推荐答案

您尝试获取项uid的方法,即:

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

DocumentSnapshot snapshot = getSnapshots().getSnapshot(holder.getAdapterPosition());
final String countryName = snapshot.getId();

仅在 FirestoreRecyclerAdapter 的情况下有效.在使用 RecyclerView.Adapter 时,必须在Model类中添加uid,然后对其进行检索.

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
}

在从firestore检索数据时,将uid字段添加到您的对象.我假设您为此使用查询,那么它将是:-

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);
        }
    }

});

onClick 的项目,您可以获取uid并传递给您的方法:-

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();

     }
});

这篇关于如何获取回收站视图项目位置的文档ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆