Room Data Doesn't Show In The Recyclerlist
It's first time using Room Data while also using MVVM pattern. The aim is that I want my data to appeard on the RecyclerList but it's doesn't shut down nor shows me any error it's
Solution 1:
You should not observe data on your repository, your activity/view should observe this data. Take a look at this:
First, add this dependency to your gradle:
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
Then in your repository
classPlantRepository(application:Application){
privateval plantDAO = PlantDatabase.getDatabase(application).plantDao()
fungetAllPlants(): Flow<List<Plant>> = plantDAO.getAll()
}
In your view model:
classPlantViewModel(
application: Application
): AndroidViewModel(application) {private var repository = PlantRepository(application)
val allPlants = repository.getAllPlants()
.flowOn(Dispatchers.IO)
.asLiveData()
}
And your activity is fine, but check your adapter, make sure that you're notifing your adapter that your list has changed. You can also work (collect) flows on your view, but this depends on you
Post a Comment for "Room Data Doesn't Show In The Recyclerlist"