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?