0

I stored all the users information in the firestore with their UID as a document. Now i create a Node as Friends in realtimeDatabase with the UID as child. Now i want to retrieve only the users whose UID is present in Friends node but the users are not showing in the recyclerView or i don't know the right way.

Firestore where all the user's info saved firestore

And this is how were node Friends created on an operation.

 FirebaseDatabase.getInstance().reference
                    .child("Friends")
                    .child(currentUserID)
                    .child(profileId)
                    .setValue("Friend")
                    .addOnSuccessListener{
    FirebaseDatabase.getInstance().reference
                            .child("Friends")
                            .child(profileId)
                            .child(currentUserID)
                            .setValue("Friend")}

firebaseDatabase

And this is how i am trying to retrieve them in a recyclerView in a fragment according to Realtime-Database

 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()
                    }
                }
            })
        }

In the userAdapter, setting up the values like this

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()
                    }
               }
         }
Stackoverflower
  • 292
  • 4
  • 17
  • You may check a relevant and detailed example in Alex Mamo's reply to the "How can I retrieve data from Firebase to my adapter" [question](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849). – George Jun 26 '20 at 15:28
  • Not useful , it is different problem. – Stackoverflower Jun 27 '20 at 04:16
  • You may choose to profit from a solution such as [FirebaseRecyclerAdapter](https://github.com/mmazzarolo/firebase-recyclerview) in GitHub, as an example. – George Jul 02 '20 at 16:13

1 Answers1

1

You have to do changes while retrieving users from, where you are retrieve users with there uid but if you think that all the information of the user will also display then it will never happen because you have just saved the uid as value in firebase try this

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()
                    
                }
            })
        }

Now you just make another constructor in your User model class with a single userId parameters

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