Reputation: 10273
I have 2 editText fields editText1 and editText2.
I am using Reactive to capture when the user changes the contents of any one of them.
When this happens, I want to copy to the other field the value of the first field and vice-versa (make them interdependent).
The problem is that when I update the second field, a new event is triggered and the consumer of the second field tries to update the first one. This results on an infinite recursion.
What I need is to take in account only the events from the user, and not the events from the setText that I perform in the consumers.
RxTextView.textChanges(editText1)
.subscribe(new Consumer<CharSequence>() {
@Override
public void accept(CharSequence e) throws Exception {
editText2.setText(editText1.getText());
}
});
RxTextView.textChanges(editText2)
.subscribe(new Consumer<CharSequence>() {
@Override
public void accept(CharSequence charSequence) throws Exception {
editText1.setText(editText2.getText());
}
});
Upvotes: 1
Views: 29
Reputation: 54204
I would try wrapping your setText()
calls in an if
check.
If you type something into TextView A, that value will propagate to TextView B, which will then update itself. This, of course, propagates back to A... but A should already have that same value anyway. So you can check and only actually call setText()
when the value is different.
public void accept(CharSequence charSequence) throws Exception {
String before = editText1.getText().toString();
String after = editText2.getText().toString();
if (!before.equals(after)) {
editText1.setText(after);
}
}
So this will still go A -> B -> A, but when the value comes back to A the second time, it will be ignored, and the infinite loop will be broken.
Upvotes: 1