如何从 firestore 检索数据并将其存储到数组 (Kotlin- Android Studio)
How to retrieve data from firestore and store it to array (Kotlin- Android Studio)
我在 firebase 中有一个数据库,其中用户集合由多个字段组成,但只有每个用户的电子邮件是必不可少的。我想从 firebase 数据库中检索它并将其存储在 ArrayList 或 hashmap 中。你能帮我在 android studio 中使用 Kotlin 实现吗?
Firebase 集合示例:
Collection:Users
->document1->email1
->document2->email2
来自 firestore 文档 - https://firebase.google.com/docs/firestore/query-data/get-data#kotlin+ktx_3
您需要创建 firestore 数据库的引用。然后从集合中获取数据,这将 return 你得到一个集合的所有文档。在迭代中将文档转换为您需要创建的用户 class,它将具有电子邮件字段。
db.collection("users")
.get()
.addOnSuccessListener { result ->
for (document in result) {
val users = document.toObject(Users::class.java)
Log.d(TAG, "${document.id} => ${document.data}")
}
}
.addOnFailureListener { exception ->
Log.w(TAG, "Error getting documents: ", exception)
}
我在 firebase 中有一个数据库,其中用户集合由多个字段组成,但只有每个用户的电子邮件是必不可少的。我想从 firebase 数据库中检索它并将其存储在 ArrayList 或 hashmap 中。你能帮我在 android studio 中使用 Kotlin 实现吗?
Firebase 集合示例:
Collection:Users
->document1->email1
->document2->email2
来自 firestore 文档 - https://firebase.google.com/docs/firestore/query-data/get-data#kotlin+ktx_3
您需要创建 firestore 数据库的引用。然后从集合中获取数据,这将 return 你得到一个集合的所有文档。在迭代中将文档转换为您需要创建的用户 class,它将具有电子邮件字段。
db.collection("users")
.get()
.addOnSuccessListener { result ->
for (document in result) {
val users = document.toObject(Users::class.java)
Log.d(TAG, "${document.id} => ${document.data}")
}
}
.addOnFailureListener { exception ->
Log.w(TAG, "Error getting documents: ", exception)
}