如何获取recyclerview Item位置的文档id?

2024-02-04

我正在使用firebaseUI使用库来填充回收器视图firestore数据库。 当我尝试检索时文档编号当我点击回收器查看项目时,它是这样的

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

我试过这个代码但是getSnapshots()出现红色字,但内部不起作用onbindeviewolder所以我尝试了这个但也不起作用

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

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

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

仅在以下情况下才有效FirestoreRecyclerAdapter。当您使用RecyclerView.Adapter您必须在模型类中添加 uid,然后检索它。

CountryItem.类

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

});

And onClick您可以获取某个项目的 uid 并将其传递给您的方法:-

holder.view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

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

     }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取recyclerview Item位置的文档id? 的相关文章

随机推荐

  • 我可以在 C++ 项目中混合使用 c 和 cpp 文件吗?

    我有一个项目 其中混合了 c 文件和 cpp 文件 链接器向我抛出错误 undefined reference to
  • 如何在C#中嵌入VBS并运行它?

    我有一个运行良好的 VBScript 我有一个 C 程序 可以使用 cscript 程序运行 VBScript 我想要做的是将 VBS 嵌入到 C exe 中 这样它就是一个文件 并且能够运行 VBS 文件 有什么方法可以将嵌入式 VBSc
  • Android 无法实例化应用程序 java.lang.ClassNotFoundException:

    我正在开发 Android 市场上托管的应用程序 有时 每月一次 我收到一份崩溃报告 无法实例化应用程序 java lang ClassNotFoundException 应用程序下载量在 10 000 50 000 之间 我不知道为什么在
  • 如何将图像添加到 JFrame 标题栏?

    我想添加一个图像 小图标 到javax swing JFrame标题栏 我该怎么做 Since JPanel没有标题栏 我假设您指的是JFrame 话虽如此 使用setIconImage http download oracle com j
  • C++11 std 相当于 Boost has_dereference

    Boost 的许多 SFINAE 助手已经随 C 11 一起出现在 std 库中 但是has dereference似乎没有 除了这个功能之外 我已经设法从我的包中消除了 Boost 依赖项 并且我想完全摆脱它 那么如何最好地仅使用 C 1
  • XML 转换和换行符

    好的 我有适用于此 xml 的代码
  • 使用java的小型http服务器?

    我使用 java 创建了以下测试服务器 import java io import java net class tcpServer public static void main String args ServerSocket s nu
  • PostgreSQL 使用组和顺序计算滚动平均值

    我有一张表如下 id x y value 1 1 1 25 1 1 2 42 1 2 3 98 1 2 4 54 1 3 5 67 2 1 1 78 2 1 2 45 2 2 3 96 我必须按 id 对其进行分组 同时按 id x 和 y
  • 尝试安装 VS2019 扩展会导致 NullReferenceException

    自 VS 2019 发布以来 我一直无法向其添加扩展 我最近更新到版本 16 4 3 但当我尝试添加扩展时仍然遇到相同的错误 我尝试执行以下操作 但仍然失败并出现上面的错误对话框 从 Visual Studio 中的扩展管理器安装 从 Ma
  • 在 NSUserDefaults Xcode 中存储和更新 Swift 字典

    我想在用户输入值时存储和更新字典 一切似乎都正常 直到这段代码 并且应用程序崩溃了 override func viewDidLoad super viewDidLoad if NSUserDefaults standardUserDefa
  • 将字体设置为斜体和粗体

    如何将多种字体样式应用于文本 System Drawing Font MyFont new System Drawing Font thisTempLabel LabelFont float thisTempLabel fontSize F
  • Hibernate 的代码优先方法

    我是一名 PHP 开发人员 正在学习 Java Spring MVC Hibernate 我想知道 Hibernate 是否支持像 Entity is ASP NET 或 Doctrine with PHP 这样的代码优先方法 到目前为止我
  • Flutter - 如何删除单个 google_maps_flutter ^0.5.21 标记?

    自成立以来发生了很大变化google maps flutter 这意味着删除单个标记的过程也发生了变化 我在这个问题的旧查询中发现了什么 删除版本 0 0 1 上的标记 https stackoverflow com questions 5
  • 如何从 Slack 机器人发送定期短暂(“隐藏”)消息

    我有兴趣编写一个 Slack 机器人 它会定期向用户发送 短暂 消息 临时消息是出现在频道内的消息 但只能由特定用户看到 例如对机器人 斜杠命令 例如 who 的响应 然而 这里的目标是让机器人定期发送临时消息 并且 出现在频道消息流中 即
  • 启动 Java Swing 桌面应用程序的正确方法

    启动需要 5 10 秒从数据库检索初始数据的应用程序的正确方法是什么 这是我到目前为止所得到的 但我不确定是否有更好的方法 我希望 GUI 和数据库访问位于不同的线程中 以便 GUI 构建与数据检索同时发生 public static vo
  • 如何解析来自 ruby​​ 客户端的 SOAP 响应?

    我正在学习 Ruby 并且编写了以下代码来了解如何使用 SOAP 服务 require soap wsdlDriver wsdl http www abundanttech com webservices deadoralive deado
  • 反序列化抽象类的集合[重复]

    这个问题在这里已经有答案了 我有一个包含 Web API 和 MVC Web 应用程序的解决方案 我的 API 有这个实体模型 有一个抽象父类和几个子类 public abstract class Person public Guid Id
  • Ruby:Proc#call 与 Yield

    以下两个 Ruby 实现之间的行为差 异是什么thrice method module WithYield def self thrice 3 times yield yield to the implicit block argument
  • 将日期 dd/mm/yyyy 格式从表单转换为时间戳?

    我有一份表格 要求填写日期dd mm yyyy格式 我尝试将其转换为时间戳strtotime 功能 但我发现只有当您在表格中填写日期时 它才有效dd mm yyyy 我该如何解决 我不知道国外的情况 但在意大利没有人这样写日期dd mm y
  • 如何获取recyclerview Item位置的文档id?

    我正在使用firebaseUI使用库来填充回收器视图firestore数据库 当我尝试检索时文档编号当我点击回收器查看项目时 它是这样的 DocumentSnapshot snapshot getSnapshots getSnapshot