android kotlin-firebase ( the nb of followers and the nb of followings are not showing on the settings activity)

Viewed 46

first i have a settings activity.kt for displaying the nb of followings and the nb of followers for each online user. also i have a fragment called searchFragment.kt that contains all the users in the app where each user is a relative layout .this fragment calls usersAdapter class to display the users in a recycler view with a follow button that turn to following when the online user follows another random user

this is the code of the method OnBindViewHolder() in UsersAdpater class:

 override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val users = mUsers[position]
        holder.usernameResources.text = users!!.getUsername()
        Picasso.get().load(users.getProfile()).placeholder(R.drawable.username).into(holder.profile)
        holder.lastMessageTxt.text = users.getBio()
        mAuth = FirebaseAuth.getInstance()
        firebaseUserId = mAuth.currentUser!!.uid
       
        holder.profile.setOnClickListener {
            val intent = Intent(mContext, ViewFullImage::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.putExtra("VisitUserId", users.getUid())
            mContext.startActivity(intent)

        }
        holder.followbutton.setOnClickListener {
            if (holder.followbutton.text == "Follow") {
                val followingRef =
                    FirebaseDatabase.getInstance("https://online-chat-541f3-default-rtdb.firebaseio.com")
                        .reference.child("Follow").child(firebaseUserId).child("Following")
                        .child(users.getUid()!!).setValue(true)
                        .addOnCompleteListener { task ->
                            if (task.isSuccessful) {
                                val followerRef =
                                    FirebaseDatabase.getInstance("https://online-chat-541f3-default-rtdb.firebaseio.com")
                                        .reference.child("Follow").child(users.getUid()!!)
                                        .child("Followers").child(firebaseUserId).setValue(true)

                            }
                        }

            } else {
                val followingRef =
                    FirebaseDatabase.getInstance("https://online-chat-541f3-default-rtdb.firebaseio.com")
                        .reference.child("Follow").child(firebaseUserId).child("Following")
                        .child(users.getUid()!!).removeValue()
                        .addOnCompleteListener { task ->
                            if (task.isSuccessful) {
                                val followerRef =
                                    FirebaseDatabase.getInstance("https://online-chat-541f3-default-rtdb.firebaseio.com")
                                        .reference.child("Follow").child(users.getUid()!!)
                                        .child("Followers").child(firebaseUserId).removeValue()


                            }
                        }

            }
        }

this code runs perfectly well and it creates the reference and the follow and following process in the firebase database

but the problem is in the settings.kt file where i want to display the nb of followings and followers this is the settings.kt code what is the problem? settings.kt code:

private var firebaseUserId:String=""
mAuth=FirebaseAuth.getInstance()
 firebaseUserId=mAuth.currentUser!!.uid
private fun getFollowing() {
            val followingRef =
                FirebaseDatabase.getInstance("https://online-chat-541f3-default-rtdb.firebaseio.com").reference.child("Follow").child(firebaseUserId).child("Following")

            followingRef.addValueEventListener(object : ValueEventListener {
                override fun onDataChange(p0: DataSnapshot) {
                    if(p0.exists()){
                        following_number.text=p0.childrenCount.toString()
                    }

                }

                override fun onCancelled(p0: DatabaseError) {

                }
            })

    }

    private fun getFollowers() {
            val followersRef = FirebaseDatabase.getInstance("https://online-chat-541f3-default-rtdb.firebaseio.com").reference.child("Follow").child(firebaseUserId).child("Followers")

            followersRef.addValueEventListener(object : ValueEventListener {
                override fun onDataChange(p0: DataSnapshot) {
                    if(p0.exists()){
                        followers_number.text=p0.childrenCount.toString()
                    }

                }

                override fun onCancelled(p0: DatabaseError) {

                }
            })

    }

N.B: followers_number is a text view that is by default 0 and displays the nb of followers following_number is a text view that is by default 0 and displays the nb of followers

1 Answers

Try out this query

   private lateinit var followersRef: DatabaseReference;
   followersRef = Firebase.database.reference.child("Follow").child(firebaseUserId).child("Followers");

   val listener = object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {

        }

        override fun onCancelled(databaseError: DatabaseError) {

        }
    }
    followersRef.addValueEventListener(listener);
Related