android - 如何在 Firestore Android 中获取文档 ID
问题描述
我试图在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
解决方案
用以下代码替换 onClick 代码段:
mFirestore.collection("Categories")
.document("tUdFCajDcQT995jX6G4k")
.collection(mCategoryTittle.get(position))
.document().get().addOnSuccessListener(documentSnapshot -> {
String id = documentSnapshot.getId();
});
推荐阅读
- python - 如何复制 Excel 工作表的内容并将其粘贴到电子邮件正文中。(不是作为附件)使用 Python
- report - Oracle SQL 开发报告错误:ORA-00942 仅在从 CMD 时返回
- java - 无法解析 JSONParser
- sql - 在 Star Schema 日期表中查找缺失的日期
- vb.net - 打开窗口并移动到下一行代码不要等待关闭窗口
- powershell - 我们如何使用 PowerShell 脚本转换 CSV 文件中的日期
- java - 在 JBoss Wildfly 中使用 EJB 和 JAR 部署 EAR - 如何从 EJB 项目中加载文件夹中的所有文件或 JAR 中的资源?
- c# - 为什么解析具有浮点值的字符串会给我点后有大量数字的数字
- android - 如何从 XML 设置或预览工具栏菜单
- sql - 如何使用连接表中的值过滤一个表中的行