我们如何获取 Firebase 实时数据库到 List
How can we fetch the Firebase realtime database to List
我在这里尝试将“用户”显示到 Recycleview 中,我需要将数据放入列表中
databaseReference?.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val list = dataSnapshot.child("users")
for (e in dataSnapshot.children) {
Log.i("messageData", e.toString())
}
Log.i("messageData", userList.toString())
}
override fun onCancelled(databaseError: DatabaseError) {
Toast.makeText(
this@MessageActivity, "database error",
Toast.LENGTH_SHORT
).show()
}
})
Realtime Database Users Sample
您可以像下面的例子一样得到列表:
databaseReference?.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val userList = dataSnapshot.children.map { snap -> User(
snap.child("Name").getValue(String::class.java)?:"",
snap.child("email").getValue(String::class.java)?:"",
snap.child("phone").getValue(Long::class.java)?:0,
) }
}
override fun onCancelled(databaseError: DatabaseError) {
Toast.makeText(
this@MessageActivity, "database error",
Toast.LENGTH_SHORT
).show()
}
})
在这里,我得到了我的解决方案
private val userList: ArrayList<UserList> = ArrayList()
databaseReference?.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (data in dataSnapshot.children) {
val model = data.getValue(UserList::class.java)
if (personalEmail != model?.email)
userList.addAll(listOf(model as UserList))
}
}
#用户列表class
data class UserList(
val email: String? = null,
val full_name: String? = null,
val phoneNumber: String? = null,
val profile_image: String? = null
)
我在这里尝试将“用户”显示到 Recycleview 中,我需要将数据放入列表中
databaseReference?.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val list = dataSnapshot.child("users")
for (e in dataSnapshot.children) {
Log.i("messageData", e.toString())
}
Log.i("messageData", userList.toString())
}
override fun onCancelled(databaseError: DatabaseError) {
Toast.makeText(
this@MessageActivity, "database error",
Toast.LENGTH_SHORT
).show()
}
})
Realtime Database Users Sample
您可以像下面的例子一样得到列表:
databaseReference?.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val userList = dataSnapshot.children.map { snap -> User(
snap.child("Name").getValue(String::class.java)?:"",
snap.child("email").getValue(String::class.java)?:"",
snap.child("phone").getValue(Long::class.java)?:0,
) }
}
override fun onCancelled(databaseError: DatabaseError) {
Toast.makeText(
this@MessageActivity, "database error",
Toast.LENGTH_SHORT
).show()
}
})
在这里,我得到了我的解决方案
private val userList: ArrayList<UserList> = ArrayList()
databaseReference?.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (data in dataSnapshot.children) {
val model = data.getValue(UserList::class.java)
if (personalEmail != model?.email)
userList.addAll(listOf(model as UserList))
}
}
#用户列表class
data class UserList(
val email: String? = null,
val full_name: String? = null,
val phoneNumber: String? = null,
val profile_image: String? = null
)