【问题标题】:Display the Friends list in a RecyclerVIew from all the users using Firebase在 RecyclerVIew 中显示所有使用 Firebase 的用户的好友列表
【发布时间】:2020-10-16 17:38:14
【问题描述】:

我将所有用户信息及其 UID 作为文档存储在 Firestore 中。现在我在 realtimeDatabase 中创建一个节点作为朋友,并将 UID 作为子节点。现在我只想检索其 UID 存在于 Friends 节点中但用户未显示在 recyclerView 中的用户,或者我不知道正确的方法。

保存所有用户信息的 Firestore

这就是在操作中创建节点好友的方式。

 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


【解决方案1】:

您必须在检索用户时进行更改,从那里检索具有 uid 的用户,但如果您认为用户的所有信息也会显示,那么它永远不会发生,因为您刚刚将 uid 作为值保存在firebase 试试这个

private fun retrieveFriends()
        {
            val usersRef = FirebaseDatabase.getInstance().reference.child("Friends")
            usersRef.addValueEventListener(object : ValueEventListener
            {
                override fun onDataChange(dataSnapshot: DataSnapshot)
                {
                    mFriend?.clear()
                           val friend = snapshot.getValue(User::class.java)
                            for each{
                            val friendID = it.key
                                mFriend?.add(User(userId = friendID)
                            }
                        friendAdapter?.notifyDataSetChanged()
                    
                }
            })
        }

现在您只需在 User 模型类中使用单个 userId 参数创建另一个构造函数

constructor(userId : String)
{ 
this.userId = userId
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 2012-09-24
    • 2021-10-03
    相关资源
    最近更新 更多