[英]Handle id of the Cloud Firestore document from RecyclerView
我正在使用 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
您可以使用从 Firestore Query生成的响应对象来获取文档 ID,这是示例代码:
String myId = response.getSnapshots().getSnapshot(position).getId();
这将从 ViewHolder 中为您提供文档的 Id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.