Reputation: 1373
I'm trying to come up with a way to have an EditText update the data of a ViewModel and simultaneously observe that data for any changes (e.g. changes brought about by manipulating the DB). Is there a way to do this without using the data binding library?
The main problem I'm facing while simply using MutableLiveData is the following:
when the user enters text in the EditText, a TextWatcher pokes the ViewModel to update its data, which in turn will set the new text to the MutableLiveData object. Because the EditText is observing the LiveData, the onChange is triggered and sets the text of the EditText accordingly, which in turn will trigger the TextWatcher again creating an infinite loop.
Upvotes: 24
Views: 15960
Reputation: 3305
In opposition to Carson Holzheimer solution I moved the validation to the UI layer (e.g. Activity or Fragment):
viewModel.textValue.observe(this, { text ->
if (myEditText.text.toString() != text) {
myEditText.setText(text)
}
})
This helped me to avoid an infinite loop.
Upvotes: 1
Reputation: 2529
I think you can also use LiveData.distinctUntilChanged(), which will only emit data when new value is set.
Upvotes: 0
Reputation: 543
May be a little late, but hope useful for somebody. I had this problem and what I did was consider a string member for the viewmodel class to maintain last value of editText. Everytime it would be to return livedata, It checks it first is there difference between new edittext input(getData() parameter) and its past value (saved in myVariable):
public class MyViewModel extends ViewModel {
private MutableLiveData<DataType> data;
private String myVariable; //maintainer variable
public LiveData<DataType> getData(String input){
if(data == null || !myVariable.equals(input)){
myVariable = input;
data = new MutableLiveData<>();
loadData(data, input);
}
return data;
}
and in your owner(eg. Activity) observe data changes:
myViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
myViewModel.getData(s).observe(this, new Observer<DataType>() {
@Override
public void onChanged(<DataType> data) {
//do everything you want like update UI by data
}
});
}
@Override
public void afterTextChanged(Editable s) {
}
});
Upvotes: 2
Reputation: 2963
I also ran into this problem since I don't like the databinding library. I did as @kAliert said, but in my ViewModel
to keep the logic there. I just added a simple catch on the function that receives my text changes events in the ViewModel
. It works well.
fun editTextChanged(newText: String) {
if (newText == textLiveData.value) {
return
}
}
Upvotes: 7