Skip to content Skip to sidebar Skip to footer

Update Object Type Livedata With Databinding

I want to update views via databinding with livedata. Lets have a look at the scenario. Data Class data class Movie(var name: String = '', var createdAt: String = '') ViewModel cl

Solution 1:

I have got the answer of my question. A hint from Here

The reference link's answer is a bit long change todo. I have got a very simple solution.

Here's what I did:

Just inherit you Data Class with BaseObservable and just call the method notifyChange() after your Object's property change from anywhere.

i.e. Data Class

  data class Movie(var name: String = "", var createdAt: String = "") : BaseObservable()

ViewModel

class MyViewModel: ViewModel(){
   var pageTitle: MutableLiveData<String>()
   var movie: MutableLiveData<Movie>()

   fun changeTitleAndMovieName()
       pageTitle.value = "Title Changed"
       movie.value.name = "Movie Name Changed"

       //here is the megic
       movie.value.notifyChange()
   } 
}

Post a Comment for "Update Object Type Livedata With Databinding"