in User repository, I am fetching the data from the database and I cast into the model class User.
How do I access two variables user and email of the model class inside the live data?
ViewModel
class MyViewModel (private val repository: UsrRepository) : ViewModel() {
private var cUser : LiveData<User> = repository.getCacheUser()
val user: LiveData<String> = cUser.user
val email: LiveData<String> = cUser.email
}
Xml
<TextView
android:text="@{viewmodel.user}"
<TextView
android:text="@{viewmodel.email}"
Since you are not doing any transformations on the data provided by cUser
, you can just make it public in your ViewModel:
val cUser: LiveData<User> = repository.getCacheUser()
Then access the user properties from the LiveData directly in the layout:
android:text="@{viewmodel.cUser.user}"
android:text="@{viewmodel.cUser.email}"
If you did not want to use the raw values, and instead some other computed values, you would instead expose a transformation of cUser
, creating a new LiveData that has values modified from the original.