[英]how do i retrieve data from firebase realtime database using kotlin
Im trying to pull data from firebase realtime database but i'm not sure how to pull more than one data piece at the same time if its possible.我试图从 firebase 实时数据库中提取数据,但我不确定如何在可能的情况下同时提取多个数据。
so this is what the database looks like:所以这就是数据库的样子:
so far i have managed to be able to print out all of these values in the following way:到目前为止,我已经设法通过以下方式打印出所有这些值:
private fun getData() {
var currentUid = mAuth.currentUser?.uid
val myRef = database.getReference("User-following").child(currentUid!!)
myRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val children = dataSnapshot.value
Log.e("Database", "value = $children")
}
override fun onCancelled(error: DatabaseError) {
Log.e("Database", error.toString())
}
})
}
when it prints the value of children i get all 5 in the following format:当它打印 children 的值时,我得到以下格式的所有 5 个值:
E/Database: value = {-MLwcu81dicGo1NezqJD=1, -MLwcwBjdjRo-vgSkEjR=1, -MLwep1w5z4DfGeabx0d=1, -MLw_sc6aHPxPpGBIpCL=1, -MLwdqVch3iDr3GXylln=1}
E/数据库:值={-MLwcu81dicGo1NezqJD=1,-MLwcwBjdjRo-vgSkEjR=1,-MLwep1w5z4DfGeabx0d=1,-MLw_sc6aHPxPpGBIpCL=1,-MLwdqVch3iDr3GXylln=1}
how to i return each individual id so that i can use it to retrieve the data that corresponds to each id?如何返回每个单独的 ID,以便我可以使用它来检索与每个 ID 对应的数据?
To access the individual child nodes under the snapshot you retrieved, you'll want to loop over its children.要访问您检索到的快照下的各个子节点,您需要遍历其子节点。
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (childSnapshot in dataSnapshot.children) {
Log.i("Database", "child key = $childSnapshot.key")
}
}
Also see the Firebase documentation on listening to value events .另请参阅有关监听值事件的 Firebase 文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.