【发布时间】:2020-01-15 19:30:49
【问题描述】:
我有一个使用 Firestore 数据库用 Kotlin 编写的文档(“卡片”)共享应用程序。用户可以创建和共享存储在子集合中的多媒体消息,如下所示:
collection("users").document(userId).collection("messages").document(cardDocID)
cardDocID 会在存储文档时自动生成。
我使用 Groupie 库支持的 RecyclerView 和 CardView(s) 存储消息、检索数据并将其显示在“CardViewActivity”中没有问题。
我的问题:我想在显示的任何消息项上实现由 onItemLongClick 事件触发的删除函数。为此,我需要获取一个 DocumentReference,类似于:
val currentDocRef: DocumentReference =
firestoreInstance.document("users/$uid/messages/$cardDocID")
如何在“CardViewActivity”中获取 cardDocID? 我使用以下代码获取数据:
messagesListenerRegistration = addCardsListener(this, userId, this::updateRecyclerView)
fun addCardsListener(context: Context, selectedUser: String, onListen: (List<Item>) -> Unit
): ListenerRegistration {
return firestoreInstance.collection("users").document(selectedUser).collection("messages")
.orderBy("time")
.addSnapshotListener { querySnapshot, firebaseFirestoreException ->
if (firebaseFirestoreException != null) {
Log.e("FIRESTORE", "Cards listener error.", firebaseFirestoreException)
return@addSnapshotListener
}
val items = mutableListOf<Item>()
querySnapshot!!.documents.forEach {
// if (it.id != FirebaseAuth.getInstance().currentUser?.uid) // to filter the currentUser
items.add(CardItem(it.toObject(Card::class.java)!!, context))
}
onListen(items)
}
}
【问题讨论】:
标签: android firebase kotlin google-cloud-firestore