You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何在FirestorePagingAdapter获取文档ID并传递至其他页面?

解决FirestorePagingAdapter获取文档ID并传递的问题

我完全理解你的困扰——FirestorePagingAdapter为了优化分页读取,确实没有提供和FirestoreRecyclerAdapter一样的getSnapshots()方法,但咱们有两种简单可行的方案来获取文档ID,实现点击跳转传参的需求:

方案1:在实体类中添加文档ID字段(推荐)

这种方法是把文档ID直接绑定到你的User实体对象里,后续使用起来非常方便:

步骤1:修改User实体类

首先给User类新增一个documentId字段(记得加上对应的getter/setter,或者用public字段):

public class User {
    public String name;
    public String status;
    public String thumb_image;
    // 新增文档ID字段
    public String documentId;

    // 空构造函数(Firestore序列化必须)
    public User() {}

    // 带参构造函数(可选)
    public User(String name, String status, String thumb_image) {
        this.name = name;
        this.status = status;
        this.thumb_image = thumb_image;
    }
}

步骤2:自定义SnapshotParser解析文档ID

在构建FirestorePagingOptions的时候,通过SnapshotParser把文档ID赋值给User对象:

Query query = FirebaseFirestore.getInstance().collection("users")
        .orderBy("name");

PagedList.Config config = new PagedList.Config.Builder()
        .setEnablePlaceholders(false)
        .setPrefetchDistance(10)
        .setPageSize(20)
        .build();

FirestorePagingOptions<User> firestorePagingOptions = new FirestorePagingOptions.Builder<User>()
        .setLifecycleOwner(this)
        .setQuery(query, config, new SnapshotParser<User>() {
            @NonNull
            @Override
            public User parseSnapshot(@NonNull DocumentSnapshot snapshot) {
                // 把文档数据转成User对象
                User user = snapshot.toObject(User.class);
                // 给User对象设置文档ID
                if (user != null) {
                    user.documentId = snapshot.getId();
                }
                return user;
            }
        })
        .build();

步骤3:在点击事件中获取并传递ID

现在在onBindViewHolder里,直接通过user.documentId就能拿到ID,传递给Intent:

holder.mView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent userProfileIntent = new Intent(UsersActivity.this, UserProfileActivity.class);
        // 直接从user对象获取文档ID
        if (user.documentId != null) {
            userProfileIntent.putExtra("user_id", user.documentId);
            startActivity(userProfileIntent);
        }
    }
});

方案2:使用FirestorePagingAdapter的getItemKey()方法

如果你不想修改User实体类,可以直接调用适配器的getItemKey()方法获取文档ID:

在点击事件里这样写:

holder.mView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent userProfileIntent = new Intent(UsersActivity.this, UserProfileActivity.class);
        // 获取当前位置的文档ID
        Object key = firestorePagingAdapter.getItemKey(position);
        if (key != null) {
            String userId = key.toString();
            userProfileIntent.putExtra("user_id", userId);
            startActivity(userProfileIntent);
        }
    }
});

注意事项

  • getItemKey()返回的是Object类型,需要转成String使用,并且要做非空判断,避免空指针异常。
  • 这个方法的底层是从分页数据源中直接获取文档的唯一标识,和文档ID是一致的。

两种方案都能解决你的问题,推荐方案1,因为它让数据模型更完整,后续在其他地方需要文档ID时也能直接使用。

内容的提问来源于stack exchange,提问作者Shubhaw Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:18:27