Why does LiveData better than MutableLiveData?
Tag : android , By : vitorcoliveira
Date : March 29 2020, 07:55 AM
Does that help It's a practice designed to restrict modification of the value from outside the class. LiveData is read-only. MutableLiveData, as the name implies, allows one to change the value it holds.
|
Using Livedata and having a problem with displaying MutableLiveData<Float> result
Date : March 29 2020, 07:55 AM
I hope this helps you . I am just testing ViewModel and LiveData of Android Architechture Components. I have this basic Activity with one EditText to receive a number from the user and when clicking the Convert button then the Textview should give the result multipled by 0.87. For example when I put 100 in EditText and click Convert button it should give the result of 87. But when I click the Convert button the Textview gives the value of "android.arch.lifecycle.MutableLiveData@eb84a7b". And when I rotate the screen then the result becomes 87. Could you please have a look at my code and tell me where I should revise the code so that the result gives the number when I click Convert button? , Use getValue() instead of toString() mTextView.setText(mViewModel.getResult().getValue());
|
Add List to MutableLiveData
Date : March 29 2020, 07:55 AM
seems to work fine I have a MutableLiveData and I want to add a List inside, but seems the below code doesn't work: , Make your variable like this: private var factsLive: MutableLiveData<List<Fact>> = MutableLiveData<>()
|
MutableLiveData<String> to MutableLiveData<Int>
Date : March 29 2020, 07:55 AM
it fixes the issue How could I convert MutableLiveData< String> to MutableLiveData< Int>. , You should use Transformations.map to get a intLiveData. val intLiveData = Transformations.map(textLiveData) {
try {
it.toInt()
} catch (e: NumberFormatException) {
0
}
}
intLiveData.observe(lifecycleOwner, Observer{ intValue ->
// get the int value.
})
|
NullPointerException when trying to use MutableLiveData value
Date : March 29 2020, 07:55 AM
this one helps. This is my ViewModel , You must init LiveData properties: class ManualInputViewModel : ViewModel() {
var username = MutableLiveData<String>()
var password = MutableLiveData<String>()
fun onClickLogin(view: View){
view.context.toast(username.value)
}
}
|