0

I don't know why my recyclerview not showing and data. println in MainActivity is working corectly

\ files/code

MainActivity file

fun retriveDataFromDatabase() : ArrayList<Users> {

    databaseRefrence.addValueEventListener(object : ValueEventListener{
        override fun onDataChange(snapshot: DataSnapshot) {

            userList.clear()

            for (eachUser in snapshot.children) {
                val user = eachUser.getValue(Users::class.java)

                if (user != null) {
                    println("user name: ${user.name}")
                    println("user email: ${user.email}")
                    println("user Id: ${user.userId}")
                    println("***************************")

                    userList.add(user)
                }

            }
            println("In fun: $userList")
        }


        override fun onCancelled(error: DatabaseError) {
            TODO("Not yet implemented")
        }
    })

    return userList
}

UserAdapter file

User.kt code

data class Users(val userId : String = "", val name : String = "", val email : String = "") { }

xMaster
  • 41
  • 5
  • You can't do it this way, trying to return an asynchronously found value from a synchronous function. See the linked question for how to do it correctly. My answer on that linked question describes exactly what your issue is here. – Tenfour04 Dec 08 '22 at 15:54
  • There is no way you can do that. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Dec 09 '22 at 08:25

0 Answers0