如何在FirestorePagingAdapter中获取文档ID?

Shubhaw Kumar:

我正在尝试使用FirestorePagingAdapter来显示我的Firestore数据库中所有用户的列表。我正在使用FirestorePagingAdapterFirestoreRecyclerAdapter代替FirestoreRecyclerAdapter来最大程度地减少读取次数,因为F irestorePagingAdapter不会同时读取整个文档列表FirestoreRecyclerAdapter我能够成功显示分页列表,但是我需要对其进行实现onClickListener,并且在单击每个项目时,我需要打开另一个活动,该活动显示了所单击的特定用户的详细说明。为此,我需要将被点击用户的documentId传递给下一个活动。

但是不幸的是,FirestorePagingAdapter没有getSnapshots()方法,因此我使用getSnapshots()。getSnapshot(position).getId()。

另一方面,FirestoreRecyclerAdapter具有此方法,这使获取文档ID变得非常容易。这样的事情:如何在Firestore db中获取Android中的文档ID或名称以传递到另一个活动?

// Query to fetch documents from user collection ordered by name
Query query = FirebaseFirestore.getInstance().collection("users")
                .orderBy("name");

// Setting the pagination configuration
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, User.class)
                .build();

firestorePagingAdapter =
                new FirestorePagingAdapter<User, UserViewHolder>(firestorePagingOptions){

                    @NonNull
                    @Override
                    public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        View view = LayoutInflater.from(parent.getContext())
                                .inflate(R.layout.single_user_layout, parent, false);

                        return new UserViewHolder(view);
                    }

                    @Override
                    protected void onBindViewHolder(@NonNull UserViewHolder holder, int position, @NonNull User user) {
                        holder.setUserName(user.name);
                        holder.setStatus(user.status);
                        holder.setThumbImage(user.thumb_image, UsersActivity.this);


                        holder.mView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent userProfileIntent = new Intent(UsersActivity.this, UserProfileActivity.class);
                                // Need to fetch the user_id to pass it as intent extra
                                // String user_id = getSnapshots().getSnapshot(position).getId();
                                // userProfileIntent.putExtra("user_id", user_id);
                                startActivity(userProfileIntent);
                            }
                        });
                    }
                };
Shubhaw Kumar:

我能够通过使用SnapshotParserin setQuery方法来做到这一点通过此操作,我能够修改从Firestore获得的对象。documentSnapshot.getId()方法返回文档ID。

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 = snapshot.toObject(User.class);
                        user.userId = snapshot.getId();
                        return user;
                    }
                })
                .build();

在User类中,我刚刚在User类中添加了另一个字段“ String userId”。我的Firestore文档中不存在userId字段。onClickListener然后,在中,我可以直接用于user.userId获取文档ID并将其发送到其他活动。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Watson Discovery中从集合中获取文档ID

如何在python搜索api中获取文档ID?

我如何在颤振中获取文档 ID

如何在集合中获取文档ID的列表而不在Firestore中获取文档的内容?

新更新后,如何在 Dart 的 Cloud Firestore 中添加文档后获取文档 ID

如何在FaunaDB中获取嵌套文档?

如何在systemtap中获取syscall文档

如何在golang中获取func文档?

如何在Netbeans中获取C文档?

如何在 WPF 中获取 WebView 的文档?

如何在Node查询中的MongoDB集合中获取具有特定名称的文档的ID

如何在集合中获取所有文档的ID并存储在字符串数组中?

如何在文档内的数组中获取文档引用

Google Firestore-如何在一次往返中通过多个ID获取文档?

Firestore-如何在React Native中通过ID获取文档

如何在Gogo的MGO mongo db驱动程序中获取我的文档的ObjectId(_id)

如何在猫鼬中获取更新文档的对象 ID?

如何在Lucene 7+中通过文档ID获取DocValue?

如何在Vue.js中获取新创建的Firestore文档的ID?

如何在猫鼬pre updateOne钩子中获取文档_id?

如何在快速点击 Firestore 中的 uitablecellviewcell 时获取文档 ID

如何在所有快照中获取具有特定 id 的所有 firestore 文档?

如何在 android studio 中获取我新创建的文档的 id?

如何在颤动中获取文档 ID 并将其存储到字段

Lucene-如何在给定父文档ID的情况下获取父模块中的所有子文档

如何在 Flutter 上获取 Cloud Firestore 上的文档 ID

如何从Firestore中快速获取集合中的文档ID

如何在python中的gensim中获取单词的最近文档

如何在 Firestore 中的文档中获取对象