使用适配器的
getSnapshots()方法:
@Overridepublic void onBindViewHolder(MainActivity.DealsHolder holder, int position, DealsResponse model) { // ... holder.itemView.setonClickListener(v -> { documentSnapshot snapshot = getSnapshots().getSnapshot(holder.getAdapterPosition()); snapshot.getId(); // ... }); }该的getId()方法返回文档的ID,所以
collection/myDoc/someField,
myDoc将是ID。
如果您在下一个活动中了解数据结构,则可以通过标准
firestore.collection("foo").document("bar")方法重新创建具有该ID的引用。如果您正在寻找一般解决方案,请使用getPath()以下方法:
fun Bundle.putRef(ref: documentReference) = putString(REF_KEY, ref.path)fun Bundle.getRef() = FirebaseFirestore.getInstance().document(getString(REF_KEY))
如果要在模型中使用ID,请使用自定义
SnapshotParser:
val options = FirestoreRecyclerOptions.Builder<DealsResponse>() .setQuery(query) { it.toObject(DealsResponse::class.java).apply { id = it.id } } .build()


