I have a method drawItems()
which everytime creates a new layout and sets it as a contentView
. And I also have a control EditText
which should remove other elements when it's content is changed.
edit.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
currentText = s.toString();
drawItems();
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
What I want to do is save its current text, remove all elements and leave only this EditText
with the saved string
. When I'm trying to run this application the error raised is StackOverflow because it renders drawItems
method infinite number of times. Why does it render drawItems within afterTextChanged
even if I don't change its content? It's rendered even before the whole page is loaded.
This little snippet might help:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
idAnswerEditText.removeTextChangedListener(this);
String someString = StringUtil.doSomething(s.toString());
idAnswerEditText.getText().clear();
idAnswerEditText.getText().append(someString);
idAnswerEditText.setSelection(someString.length());
idAnswerEditText.addTextChangedListener(this);
}
});
When you edit the String while the user is typing, remove the listener and do whatever you need to do on the text, then reattach the listener to the EditText.
This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively. (You are not told where the change took place because other afterTextChanged() methods may already have made other changes and invalidated the offsets. But if you need to know here, you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) to mark your place and then look up from here where the span ended up.
put:
public void afterTextChanged(Editable s) {
if (MyEditText.getText().toString().compareTo(s.toString()) != 0)
{
// your code ...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With