I have an annoying problem with TextWatcher. i've been searching the web but couldnt find anything. appreciate if someone could assist me.
For some reason the calls to the TextWatcher events upon one text change are erratic. sometimes they are being triggered once (like they should be), sometimes twice, and sometimes 3 times. have no idea why, the whole thing is very straight forward. also sometimes the Editable parameter on afterTextChanged() returns empty values in toString() and length().
code is below:
private TextWatcher mSearchAddressTextChangeListener = 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 searchedAddress) { System.out.println("called multiple times."); } };
inside afterTextChanged()
(and the AsyncTask
) im not making any change to the text or the EditText
view.
i saw the question asked in Events of TextWatcher are being called twice, but im having the events triggered more (or less) than twice.
anyway, appreciate any help.
EDIT: I removed the content of afterTextChanged() cause this problem is happening even without my code. what leads me to believe this is a bug. The bug is occuring when a 'space' char is entered right after a regular char (event handlers are triggered twice) or when a 'space' char after a regular char is removed (backspace. event handlers are triggered 3 times). help will still be appreciated.
According to the developer pages for TextWatcher, if a change is made to the Editable within TextWatcher, it will trigger further calls to all the TextWatchers linked to that Editable. Now, clearly your code doesn't trigger this behaviour.
So, for some reason, eventListener is firing twice instead of one when I try second time. How to resolve this issue? This symptom is indicative that you've registered the same listener more than once. You must remember to deregister events when your component unloads to prevent his problem. This is done with aura:valueDestroy.
However, it is quite possible that if, for whatever reason, the system has a TextWatcher on the Editable, the situation you describe can occur. "Why", I hear you cry, "should this happen?" First, the classic defence: there is no reason for it not to happen and, strictly, app code should be written to be resilient to it.
Don't ask me why, but it worked, afterTextChanged is now called only once. This happens because in cases where the user types a space, certain devices will capitalize the previous word (if it is the first word and inputType="name"). Therefore, afterTextChanged gets called twice.
I had the same kind of problem, when I pressed backspace with cursor at the end of a continuous text, afterTextChange was called 3 times: - The first time with the correct s value - The second time with a clear value - The third time with the correct value again
After having search a lot on the web, I tried to change my EditText inputType to
android:inputType="textNoSuggestions"
Don't ask me why, but it worked, afterTextChanged is now called only once.
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