有没有办法从FirestoreRecyclerAdapter获取项目的文档ID? [英] Is there a way to get document ID of item from FirestoreRecyclerAdapter?

查看:39
本文介绍了有没有办法从FirestoreRecyclerAdapter获取项目的文档ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以手动查询Firestore并获取ID,但是我使用的是 FirestoreRecyclerAdapter ,因为它附带了许多额外的开箱即用的工作.所以,如果我有这样的代码:

I can query Firestore and get IDs manually, but I'm using FirestoreRecyclerAdapter because it comes with a ton of additional work done out of the box. So, if I have code like this:

FirebaseFirestore db = FirebaseFirestore.getInstance();

Query query = db.collection(COLLECTION_NAME)
        .whereEqualTo("userId", USER_ID)
        .limit(LIMIT);

FirestoreRecyclerOptions options = new FirestoreRecyclerOptions
        .Builder<SomeEntity>()
        .setQuery(query, SomeEntity.class)
        .build();

FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<SomeEntity, MyHolder>(options) {
    @Override
    public void onBindViewHolder(MyHolder holder, int position, SomeEntity model) {
        // I need model ID here
    }
}

因此,如果我使用 FirestoreRecyclerAdapter ,它将自动将我的 SomeEntity 类反序列化为 model 对象.这一切都很好,但现在我需要将侦听器附加到列表中,并使用模型ID做出反应.现在,如果要手动从Firestore反序列化对象,则可以执行以下操作:

So, if I use FirestoreRecyclerAdapter, it automatically deserializes my SomeEntity class into model object. This is all fine and dandy, but now I need to attach listeners to the list, and react with model ID. Now, if I was manually deserializing objects from Firestore I'd do something like this:

for (DocumentSnapshot document : task.getResult()) {
    SomeEntity entity = document.toObject(SomeEntity.class).setId(document.getId());
    list.add(entity);
}

但是,由于 FirestoreRecyclerAdapter 正在为我进行反序列化,所以我自己无法调用 document.getId().到我进入 onBindViewHolder 方法时,我再也无法访问文档ID.有什么我缺少的方法,有什么方法可以从我忽略的适配器中检索ID?

But, since FirestoreRecyclerAdapter is doing deserialisation for me, I don't get to call document.getId() myself. And by the time I'm in onBindViewHolder method, I don't have access to document ID anymore. Is there some method I'm missing, some way to retrieve IDs from adapter that I overlooked?

请注意,我不认为将ID冗余存储为字段是一种解决方案.我宁愿继承并重写 FirestoreRecyclerAdapter ,但是我更愿意在没有太多工作的情况下解决这个问题.

Note, I do not consider redundantly storing IDs as a field a solution. I will rather inherit and override FirestoreRecyclerAdapter instead, but I'd prefer if I could solve this without that much work.

推荐答案

当您需要文档ID时,您应该已经知道要处理的项目.通常,这将是项目在列表中的位置.IE.当用户单击某个项目时,会将该项目的ID传递给点击处理程序.如果具有 position ,则可以使用以下方法从适配器获取 DataSnapshot :

When you need the document ID, you should know something already about the item you're dealing with. Typically this will be the position of the item in the list. I.e. when the user clicks on an item, the click handler is passed the ID of that item. If you have the position, you can get the DataSnapshot from the adapter with:

adapter.getSnapshots().getSnapshot(position);

因此,如果您想获取ID,可以通过以下方式获取它:

So if you want to get the ID, you'd get it with:

String id = adapter.getSnapshots().getSnapshot(position).getId();

这篇关于有没有办法从FirestoreRecyclerAdapter获取项目的文档ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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