简体   繁体   English

从 RecyclerView 处理 Cloud Firestore 文档的 ID

[英]Handle id of the Cloud Firestore document from RecyclerView

I'm doing deletion with Cloud Firebase.我正在使用 Cloud Firebase 进行删除。 I have a code that delete the document:我有一个删除文档的代码:

db.collection ( "cities"). document ( "DC")
        .Delete ()
        .addOnSuccessListener (new OnSuccessListener <Void> () {
            @Override
            public void onSuccess (Void aVoid) {
                Log.d (TAG, "DocumentSnapshot successfully deleted!");
            }
        })
        .addOnFailureListener (new OnFailureListener () {
            @Override
            public void onFailure (@NonNull Exception e) {
                Log.w (TAG, "Error deleting document", e);
            }
        });

I need to get the id value of the document that is currently in RecyclerView.我需要获取当前在 RecyclerView 中的文档的 id 值。 I would like it to work with onLongPress.我希望它与 onLongPress 一起使用。

To handle item on long click on recycler view - I found this code:要在回收站视图上长按处理项目 - 我找到了这个代码:

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
    private Article article;

    private TextView nameTextView;

    public ViewHolder (View itemView) {
        super (itemView);
        itemView.setOnClickListener (this);
        itemView.setOnLongClickListener (this);
        nameTextView = (TextView) itemView.findViewById (R.id.grid_item_article_name_textView);
    }

    public void bind (Article article) {
        this.article = article;
        nameTextView.setText (article.getName ());
    }

    @Override
    public void onClick (View view) {
        // Context context = view.getContext ();
        // article.getName ()
    }

    @Override
    public boolean onLongClick (View view) {
        // Handle long click
        // Return true to indicate the click was handled
        return true;
    }
}

How to get document id from Firestore?如何从 Firestore 获取文档 ID?

Edit: MainFragment - https://codepaste.net/idih7i MainActivity - https://codepaste.net/9q019e编辑:MainFragment - https://codepaste.net/idih7i MainActivity - https://codepaste.net/9q019e

You can the fetch document ID by using a response object you generate from the Firestore Query , Here is sample code for that:您可以使用从 Firestore Query生成的响应对象来获取文档 ID,这是示例代码:

String myId = response.getSnapshots().getSnapshot(position).getId();

This will give you the Id of the document from inside the ViewHolder.这将从 ViewHolder 中为您提供文档的 Id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM