【发布时间】:2020-10-16 17:38:14
【问题描述】:
我将所有用户信息及其 UID 作为文档存储在 Firestore 中。现在我在 realtimeDatabase 中创建一个节点作为朋友,并将 UID 作为子节点。现在我只想检索其 UID 存在于 Friends 节点中但用户未显示在 recyclerView 中的用户,或者我不知道正确的方法。
这就是在操作中创建节点好友的方式。
FirebaseDatabase.getInstance().reference
.child("Friends")
.child(currentUserID)
.child(profileId)
.setValue("Friend")
.addOnSuccessListener{
FirebaseDatabase.getInstance().reference
.child("Friends")
.child(profileId)
.child(currentUserID)
.setValue("Friend")}
这就是我试图根据实时数据库在片段中的 recyclerView 中检索它们的方式
private fun retrieveFriends()
{
val usersRef = FirebaseDatabase.getInstance().reference.child("Friends")
usersRef.addValueEventListener(object : ValueEventListener
{
override fun onDataChange(dataSnapshot: DataSnapshot)
{
mFriend?.clear()
for (snapshot in dataSnapshot.children)
{
val friend = snapshot.getValue(User::class.java)
if (friend != null)
{
mFriend?.add(friend)
}
friendAdapter?.notifyDataSetChanged()
}
}
})
}
在 userAdapter 中,像这样设置值
private fun friendInfo(fullName: TextView, about: TextView, location: TextView, profileImage: CircleImageView) {
val pref = mContext.getSharedPreferences("PREF", Context.MODE_PRIVATE)
if (pref != null) {
this.profileId = pref.getString("profileId", "none").toString()
}
val userRef = FirebaseFirestore.getInstance().collection("Users").document(profileId)
userRef.get()
.addOnSuccessListener { documentSnapshot ->
if (documentSnapshot != null && documentSnapshot.exists()) {
val user = documentSnapshot.toObject(User::class.java)
Picasso.get().load(user!!.getImage()).placeholder(R.drawable.default_pro_pic).into(profileImage)
fullName.text = user.getFullName()
about.text = user.getAbout()
location.text = user.getLocation()
}
}
}
【问题讨论】:
-
您可以查看 Alex Mamo 对“如何将数据从 Firebase 检索到我的适配器”question 的回复中的相关详细示例。
-
没用,是不同的问题。
-
您可以选择从GitHub中的FirebaseRecyclerAdapter等解决方案中获利,例如。
标签: android kotlin firebase-realtime-database android-recyclerview google-cloud-firestore