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
class MyViewModel: ViewModel(){
var pageTitle: MutableLiveData<String>()
var movie: MutableLiveData<Movie>()
fun changeTitleAndMovieName()
pageTitle.value = "Title Changed"
movie.value.name = "Movie Name Changed"
}
}
XML
<layout>
...
<TextView
...
android:text="@{`Title: `+viewmodel.pageTitle `Name: `+viewmodel.movie.name}"/>
<Button
...
android:onClick="@{() -> viewmodel.changeTitleAndMovieName()}"/>
</layout>
What I want to do?
- When the button is pressed, the title and the name of movie should change and reflect to the view.
What is happening now?
- Only page title is changing because of String type LiveData.
- Movie name is NOT being reflected in the view because of Movie type LiveData and I am changing the property of Movie type LiveData's property.
Is there any way to update Movie type LiveData to the view when any property is changed of the Movie.
I dont want to re-assign the object to the livedata e.g. viewmodel.movie.value = Movie(...)