Note that there are some explanatory texts on larger screens.

plurals
  1. POTextWatcher.afterTextChanged has incorrect string after backspace
    primarykey
    data
    text
    <p>I am using a TextWatcher to listen to key inputs. When user types '@', I open up a listactivity and user has to choose from the list. Once choosen, I place the selected item's text(including the initial @) to the edittext and normal editing goes on. </p> <p>The issue is that when I press backspace, the string I get in aftertextchanged event is wrong, and the listactivity again pops up.</p> <pre><code>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) { String str = s.toString(); if (str.length() &gt; 0) { if (str.substring(str.length() - 1).equals("@")) { Intent i = new Intent(MessageComposeActivity.this, MembersListActivity.class); startActivityForResult(i, Util.MEMBERS_LIST); } } } }); </code></pre> <p>And in onActivityResult:</p> <pre><code>@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == Util.MEMBERS_LIST) if (resultCode == RESULT_OK) { editText.setText(editText.getText().toString() + data.getExtras().get("screenname") + " "); editText.setSelection(editText.getText().length()); } } </code></pre> <p><strong>For example:</strong></p> <p>In EditText I type '@', and the activity pops up and I choose 'James'. The EditText now shows @James. If I press backspace once or twice, the listactivity again pops up while the EditText shows @Jam.</p> <p>PS: The afterTextChanged() is called twice sometimes for a backspace (or any key), on 2nd execution of afterTextChanged() I get wrong input string. On first execution of afterTextChanged() I get @Jam, and on 2nd execution I get '@' hence listactivity pops up.</p> <p><strong>Question:</strong> Why afterTextChanged() is called twice, AND why on 2nd execution I get wrong text?</p> <p>Thanks a lot.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload