I have an autocompletetextview on it, I hung the listener addTextChangedListener , and directly passed it to TextWatcher . The problem is the following I need to get the character immediately after entering That is, the person entered the letter, I need to read it and send it to the north, here is an example of my code. I do not understand why it does not work out.

  private void initActCity() { adapterActCity = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, cityNameList); actCity.setAdapter(adapterActCity ); actCity.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { actCity.showDropDown(); actCity.requestFocus(); return false; } }); actCity.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }@Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.length()>0) { int a = actCity.getText().length(); if (a == 0) { cityNameList.clear(); } else if (a == 1) { // Сдесь если есть символ то получить его и отправить. } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } } @Override public void afterTextChanged(Editable s) { } }); 

I also tried to add a simple TextView and pass the value from the onTextChanged method as follows textview.setText(actCity.getText); - the textView didn’t lead to textView empty, I understand that it doesn’t work somewhere, I tried to go under debug, I can’t pass this place, it passes it. Help me please!

    2 answers 2

    It is not necessary to check for length

     int a = actCity.getText().length(); 

    and here it is

     CharSequence s 

    The character in onTextChanged appears earlier than in the input field itself, so actCity.getText () is empty at this moment.

      Transfer the code from onTextChanged(CharSequence s, int start, int before, int count) to afterTextChanged(Editable s) . You will receive the entered characters as follows:

       final String stroka = s.toString(); 

      where s is editable