Skip to content Skip to sidebar Skip to footer

Data Binding - Access Individual Properties Contained In Livedata

Is there a way to do something like this with LiveData and data binding? ViewModel has this property: val weather: LiveData What I'm trying

Solution 1:

From the point of view of MVVM pattern it's not entirely correct. In your example view require know about property path to display data. Preferable to provide target data directly from ViewModel. If your property is depend from another, you can use Transformations:

val weather: LiveData<UnitSpecificCurrentWeatherEntry> = //suppose, we have instantiation here
val someProperty: LiveData<SomePropertyType> = Transformations.map(weather) { it.someProperty }

Now, you can use it in your xml:

<TextView
    android:id="@+id/textView"
    android:text="@{viewmodel.someProperty}"/>

Post a Comment for "Data Binding - Access Individual Properties Contained In Livedata"