TextWatcher afterTextChanged calls stackoverflow in android

I have a drawItems() method that creates a new layout each time and sets it as a contentView . And I also have an EditText control that should delete other elements when the content changes.

 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, delete all the elements and leave only the EditText with the saved string . When I try to run this application, a StackOverflow error occurs because it makes the drawItems method drawItems infinite number of times. Why does it drawItems inside afterTextChanged , even if I do not change its contents? It is displayed even before the entire page loads.

+4
source share
3 answers

This method is called to notify you that somewhere in s the text has been changed. You can make further changes to this callback, but be careful not to end up in an infinite loop, because any changes you make will call this method again recursively. (You did not say where the change occurred because the other afterTextChanged () methods may have already 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 from here where the range ends.

set:

 public void afterTextChanged(Editable s) { if (MyEditText.getText().toString().compareTo(s.toString()) != 0) { // your code ... } } 
+3
source

As @ DroidIn.net said in a comment, you are probably triggering a text change event in your afterTextChanged processing - possibly in drawItems ().

You can resolve the exception by using Handler to defer processing of drawItems, but that will probably just lead you into an infinite loop. You should look at drawItems and make sure that they do not change your edit text, as a result of which the change will change the edit text, etc.

+1
source

This small snippet may 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 a line during user input, delete the listener and do whatever you need to do in the text, and then reconnect the listener to the EditText.

-1
source

Source: https://habr.com/ru/post/1387918/


All Articles