Home:ALL Converter>Android Kotlin Firebase realtime database: RecyclerView shows empty result

Android Kotlin Firebase realtime database: RecyclerView shows empty result

Ask Time:2018-06-08T21:57:55         Author:GPH

Json Formatter

My RecyclerView is working when I use dummy data, but displays an empty result for RecyclerView when connected to Firebase database. Please help to solve this issue.

override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        var adapter = qponAdapter(generateData())
        val layoutManager = LinearLayoutManager(context)
        qponRecyclerView?.layoutManager = layoutManager
        qponRecyclerView?.itemAnimator = DefaultItemAnimator()

        qponRecyclerView?.adapter = adapter
        adapter.notifyDataSetChanged()
}

private fun generateData(): ArrayList<Company> {

    var companyReg = Company(name = "qqqqq", br = "", country = "", city = "", address = "", tel = "", value = "", qponQuantity = 0, email = "", advApproved = "false", advPassword = "", startDate = "", endDate = "", conditions = "", postApproved = "false", order = 10000, authPhone = "",nodeID = "")
    companyList.add(companyReg)
    companyList.add(companyReg)
    companyList.add(companyReg)
    companyList.add(companyReg)
    companyList.add(companyReg)
}

It is working well and display 5 record in Recyclerview.

private fun generateData(): ArrayList<Company> {
            val postListener = object : ValueEventListener {
                override fun onDataChange(dataSnapshot: DataSnapshot) {
                    // Get Post object and use the values to update the UI
                    //val post = dataSnapshot.getValue<Post>(Post::class.java!!)
                    // ...
                    val td = dataSnapshot.value as HashMap<*, *>



                    for (key in td.keys){
                        val post = td[key] as HashMap<*, *>

                        val companyReg = Company(name = "", br = "", country = "", city = "", address = "", tel = "", value = "", qponQuantity = 0, email = "", advApproved = "false", advPassword = "", startDate = "", endDate = "", conditions = "", postApproved = "false", order = 10000, authPhone = "",nodeID = "")

                        companyReg.name = post["Name"].toString()
                        companyReg.address = post["Address"].toString()
                        companyReg.country = post["Country"].toString()
                        companyReg.city = post["City"].toString()
                        companyReg.tel = post["Tel"].toString()
                        companyReg.value = post["qponValue"].toString()
                        //companyReg.qponQuantity = qponQuantity
                        companyReg.email = post["Email"].toString()
                        companyReg.advPassword = post["advPassword"].toString()

                        companyReg.startDate = post["startDate"].toString()
                        companyReg.endDate = post["endDate"].toString()
                        companyReg.conditions = post["Conditions"].toString()

                        companyReg.postApproved = post["PostApproved"].toString()
                        companyReg.advApproved = post["advApproved"].toString()
                        //companyReg.order = post["Order"] as! Int
                        companyReg.authPhone = post["authPhone"].toString()
                        companyReg.nodeID = key.toString()

                        println(companyReg.nodeID)
                        println(companyReg.address)


                        companyList.add(companyReg)
                    }


                }

                override fun onCancelled(databaseError: DatabaseError) {
                    // Getting Post failed, log a message
                    Log.w(TAG, "loadPost:onCancelled", databaseError.toException())
                    // ...
                }
            }
            myRef.addValueEventListener(postListener)


        return companyList
    }

It connected to Firebase database but the RecyclerView display 0 record. I am sure it is connecting to the database because it print out every record in the console.

Author:GPH,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762298/android-kotlin-firebase-realtime-database-recyclerview-shows-empty-result
Alex Mamo :

You cannot return something now that hasn't been loaded yet. With other words, you cannot simply use that companyList list outside the onDataChange() function because it will always be empty due the asynchronous behaviour of this function. This means that by the time you are trying to use that list outside that function, the data hasn't finished loading yet from the database and that's why is not accessible.\n\nA quick solve for this problem would be to use that list only inside the onDataChange() function, otherwise I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding. It is for Android but the same principle applies.",
2018-06-08T14:09:26
yy