Observe One Livedata Into Other Livedata Observr
I have two LiveData: MutableLiveData -User could choose number by taping at '+' and '-' buttton. LiveData> - it's my data from RoomData base by call method getLesson
Solution 1:
First in your viewmodel
define a liveData
like this.
privateval dayOfWeekChangRequest = MutableLiveData<Int>()
and a method like this, when + , - clicked, call this method.
fundayOfWeekChanged(dayOfWeek: Int) {
dayOfWeekChangRequest.value = dayOfWeek
}
now add a live data for lessonsOfThatDay
val lessonsForThatDay: LiveData<List<Lesson>> = Transformations
.switchMap(dayOfWeekChangRequest) { day ->
yourDataBaseRepository.getLessonsOfThatDay(day)
}
with this transformation, every time you change your dayOfWeek
, the lessonsForThatDay
value will be changed.
at last in your activity observe it
viewModel.lessonsForThatDay.observe(viewLifecycleOwner, Observer {
lessons ->
adapter.updateData(lessons)
subjectsListTimetableRecyclerView.layoutManager = LinearLayoutManager(context)
subjectsListTimetableRecyclerView.adapter = adapter
})
Solution 2:
You can do viewModel = Dao.getLessonsForThatDay(number)
. This way, the viewmodel will listen to changes in Dao
Post a Comment for "Observe One Livedata Into Other Livedata Observr"