Closed Diy2210 closed 4 years ago
text = const ...
There is should be something like:
text = viewModel.updates.map{"Available: $it"}
text = const ...
There is should be something like:text = viewModel.updates.map{"Available: $it"}
Tnx! And one more question, how use MutableLiveData type Array(String)
LiveData
and MutableLiveData
is just reactive data holders.
you can create LiveData with List, or Array of strings code:
private val _updates: MutableLiveData<List<String>> = MutableLiveData(emptyList())
val updates: LiveData<List<String>> = _updates
and later you can map it to any other value. for example if you want show count of items, do:
val updatesCount: LiveData<Int> = updates.map { list: List<String> ->
list.count
}
or if you want join string:
val updatesString: LiveData<String> = updates.map { list: List<String> ->
list.joinToString()
}
to change value you just set value in MutableLiveData
:
_updates.value = listOf("first", "second")
to listen changes you can use:
updates.addObserver { data ->
println("data: $data")
}
it called in each widget
Awesome, tnx!
I try use MutableLiveData with Model, but it not work for me. My Model:
My Screen Model:
And use in Screen:
And I dont understand how use MutableLiveData type - Int. I try get value like this:
But its not work:
And one more question, how use MutableLiveData type Array.