【问题标题】:Handle id of the Cloud Firestore document from RecyclerView来自 RecyclerView 的 Cloud Firestore 文档的句柄 ID
【发布时间】:2018-07-27 10:37:56
【问题描述】:

我正在使用 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 一起使用。

要在长按回收站视图时处理项目 - 我找到了这段代码:

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 MainActivity - https://codepaste.net/9q019e

【问题讨论】:

  • 嗨@Kamil,我们需要查看您的架构和模型类来帮助您:)
  • @LeviAlbuquerque 你好兄弟。我在顶部添加了链接!架构:rootRef.collection("goalsData").document(userEmail).collection("dailyGoals").document我要删除的
  • 嘿,最后一件事,你能发布你的适配器的完整代码吗?
  • afk。我晚上送你!
  • 您的问题与之前的类似,所以这个答案可以帮助您Here

标签: java android firebase android-recyclerview google-cloud-firestore


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2020-07-02
    • 2021-07-10
    • 2021-05-24
    • 2023-01-01
    • 2020-11-15
    相关资源
    最近更新 更多