Home > Net >  Android RecyclerView DB(Firebase) Question, Kotlin
Android RecyclerView DB(Firebase) Question, Kotlin

Time:01-06

My app is an app that gets data from Firebase, and then shows the data in RecyclerView. I had a few problems and solved the problems by searching, but I have some questions.

First, here is my code.

val productList = mutableListOf<Product>()
database.get().addOnSuccessListener {
    for (item in it.children) {
        productList.add(Product(item.key.toString(), item.child("name").value.toString(), item.child("photo").value.toString(), item.child("content").value.toString(), decimal.format(item.child("price").value.toString().toInt()).toString()   " P", item.child("price").value.toString().toInt()))
    }
    binding.pointMallHomeRecyclerView.adapter?.notifyDataSetChanged() // THIS POINT IS THAT I'M CURIOUST ABOUT
}

val myDataset = productList
val adapter = PointMallMainItemAdapter(requireContext(), myDataset, object: PointMallMainItemAdapter.OnItemClickListener {
    override fun onItemClick(v: View, position: Int, id: String, content: String, photo: String) {
        val action = PointMallHomeFragmentDirections.actionPointMallHomeFragmentToPointMallItemFragment(productId = id, content = content, photo = photo)
        findNavController().navigate(action)
    }
})
binding.pointMallHomeRecyclerView.adapter = adapter

When I coded it first, there isn't the

binding.pointMallHomeRecyclerView.adapter?.notifyDataSetChanged()

That way, there are data in the 'productList', but nothing appears in the RecyclerView.

But, after I added that code, it works. Why does it happen? Doesn't the code work sequentially? Is it because connecting the adapter runs faster than getting data from the DB? If it works sequentially, if the app gets all the data, it connects the adapter, right?

I'm a student developer, and just in case it helps I solve this problem, I'm studying coroutine. If my English is terrible, I'm sorry about that. Because I'm Korean.

.....................

CodePudding user response:

There is nothing displayed in your RecyclerView because your productList is empty when you're using:

val myDataset = productList

Why? Because the Firebase API is asynchronous. And it's normal behavior since reading a list of objects takes time. The solution is simple and always the same, any code that needs data from a Firebase database needs to be inside the onSuccess() method, or be called from there. In your particular case, you have to move the above line of code right inside the callback:

val productList = mutableListOf<Product>()
database.get().addOnSuccessListener {
    for (item in it.children) {
        productList.add(Product(item.key.toString(), item.child("name").value.toString(), item.child("photo").value.toString(), item.child("content").value.toString(), decimal.format(item.child("price").value.toString().toInt()).toString()   " P", item.child("price").value.toString().toInt()))
    }
    val myDataset = productList //           
  • Related