Home > Back-end >  Value Event Listener returning data of only child of Root Node
Value Event Listener returning data of only child of Root Node

Time:09-23

I am trying to get data from firebase database. the realtime database has following json data

{"Users": {
"m7jnhJgBg3etM7Tmq1YUyV8C4F83": {
  "-NCcfP3bwRGtQnMWdKbN": {
    "avery": "aa",
    "birdage": "aa",
    "birdname": "aa",
    "birdring": "aa",
    "desc": "aa",
    "phonno": "aa"
  }
},
"yIp99CWgApYzuU7lMZC3UwrpFIl1": {
  "-NCclsCkUA4xIA_T7yn3": {
    "avery": "bb",
    "birdage": "bb",
    "birdname": "bb",
    "birdring": "bb",
    "desc": "bb",
    "phonno": "bb"
  },
  "-NCclxfFhRNaQJKzYlKw": {
    "avery": "bb",
    "birdage": "bb",
    "birdname": "bb",
    "birdring": "bb",
    "desc": "bb",
    "phonno": "bb"
  }
}
}}

The m7jnhJgBg3etM7Tmq1YUyV8C4F83 further can have multiple entries.

Below is the Function for Retriving data from User's child node m7jnhJgBg3etM7Tmq1YUyV8C4F83.

private fun getuserdata() {
    databaseReference = FirebaseDatabase.getInstance().getReference("Users").child("m7jnhJgBg3etM7Tmq1YUyV8C4F83")
    databaseReference.addValueEventListener(object : ValueEventListener{
        override fun onDataChange(snapshot: DataSnapshot) {
            if (snapshot.exists()){
                for (usersnapshot in snapshot.children){
                    val user = usersnapshot.getValue(User::class.java)
                    userArrayList.add(user!!)
                }
                adapter = MyAdapter(userArrayList)
                userRecyclerView.adapter = adapter
                adapter.notifyDataSetChanged()
            }
        }
        override fun onCancelled(error: DatabaseError) {
            TODO("Not yet implemented")
        }
    })
}    

The above function is working fine but when I try to retrieve data for all the users the function is returning null. This is the code for getting complete data.

private fun getuserdata() {
    databaseReference = FirebaseDatabase.getInstance().getReference("Users")
    databaseReference.addValueEventListener(object : ValueEventListener{
        override fun onDataChange(snapshot: DataSnapshot) {
            if (snapshot.exists()){
                for (usersnapshot in snapshot.children){
                    val user = usersnapshot.getValue(User::class.java)
                    userArrayList.add(user!!)
                }
                adapter = MyAdapter(userArrayList)
                userRecyclerView.adapter = adapter
                adapter.notifyDataSetChanged()
            }
        }
        override fun onCancelled(error: DatabaseError) {
            TODO("Not yet implemented")
        }
    })
}

CodePudding user response:

Your code is not working because you are trying to convert all users node into single user POJO ,
Just include for loop and get single user node

fun getDataFromFirebase(){
    FirebaseDatabase.getInstance().getReference("Users").addValueEventListener(object : ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {
            for (allUsers in snapshot.children){
                for (singleUser in allUsers.children){
                    singleUser.getValue(User::class.java)?.let {
                        userArrayList.add(it)
                    }
                }
                adapter = MyAdapter(userArrayList)
                userRecyclerView.adapter = adapter
                adapter.notifyDataSetChanged()
            }
        }

        override fun onCancelled(error: DatabaseError) {
            Log.d("TAG", "onCancelled: ${error.message}")
        }

    })
}
  • Related