How To Load Data In Recyclerview?
Solution 1:
First of all make sure you have layoutManager
set on the RecyclerView
.
The problem here is Your ProductAdapter
never had the data . notifyDataSetChanged
is not a magic stick to notify the adapter you modify/add/update the dataset and then You will call notifyDataSetChanged
. that's how it works .
In your case You have movies
list your adapter but you never assigned anything to it its always empty .
There are several ways to it. Just to make it work You can have a method to add the data in your adapter and then notify it.
fun addData(data:List<MobileList>){
movies.addAll(data)
notifyDataSetChanged()
}
Now when you get the data inside on change you call this method .
productViewModel.products.observe(this,{
it?.let{ items ->
adapter.addData(items)
}
})
This should work .
Update on type fix - Seems like your type is messed up . Why is your repo returns a object of MobileList
? While you are using a list of MobileList
in adapter . Your adapter should hold var movies = mutableListOf<Products>()
.
productViewModel.products.observe(this,{
it?.let{ item ->
adapter.addData(item.products)
}
})
Post a Comment for "How To Load Data In Recyclerview?"