gpt4 book ai didi

java - 来自 RecyclerView 的 Cloud Firestore 文档的句柄 ID

转载 作者:行者123 更新时间:2023-11-30 10:19:08 27 4
gpt4 key购买 nike

我正在使用 Cloud Firebase 进行删除。我有一个删除文档的代码:

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

我需要获取当前在 RecyclerView 中的文档的 id 值。我希望它能与 onLongPress 一起使用。

要处理长按回收站 View 的项目 - 我找到了这段代码:

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

如何从 Firestore 获取文档 ID?

编辑:MainFragment - https://codepaste.net/idih7i主要 Activity - https://codepaste.net/9q019e

最佳答案

您可以使用从 Firestore Query 生成的response 对象获取文档 ID,以下是示例代码:

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

这将从 ViewHolder 中为您提供文档的 ID。

关于java - 来自 RecyclerView 的 Cloud Firestore 文档的句柄 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48828131/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com