[英]How to get the document ID in firestore Android
我试图在firestore中获取文档的文档ID,但是我从某个我不知道它从哪里获取这个随机ID以及为什么的地方获得了一个随机生成的ID。我试图在我的代码的recyclerAdapter中查询它:
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
Log.d(TAG, "onCreateViewHolder: Called");
Glide.with(mContext)
.asBitmap()
.load(mCategoryImages.get(position))
.into(holder.CategoryImageView);
holder.CategoryTextView.setText(mCategoryTittle.get(position));
holder.CategoryTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mFirestore = FirebaseFirestore.getInstance();
// Get the 50 highest items
String id = mFirestore.collection("Categories")
.document("tUdFCajDcQT995jX6G4k")
.collection(mCategoryTittle.get(position))
.document().getId();
Toast.makeText(mContext, id, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onClick: DocumentID: " + id);
}
});
}
我尝试对集合名称进行硬编码,即使在那之后它也得到了一些随机 ID!
我单击相同项目名称但在我的控制台2020-04-06 23:23:05.413 21856-21856/? D/CategoryMainListAdapter: onClick: DocumentID: qB79K0LsLllg28pzSyPy 2020-04-06 23:23:07.618 21856-21856/? D/CategoryMainListAdapter: onClick: DocumentID: uDumu9NngxsTmtCRuJUs 2020-04-06 23:23:08.705 21856-21856/? D/CategoryMainListAdapter: onClick: DocumentID: VmHxk0eUR9mZic5Mrgqc
如果你添加
.document().get()
总是会得到一个新的文档 ID
用以下代码替换 onClick 代码段:
mFirestore.collection("Categories")
.document("tUdFCajDcQT995jX6G4k")
.collection(mCategoryTittle.get(position))
.document().get().addOnSuccessListener(documentSnapshot -> {
String id = documentSnapshot.getId();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.