Search code examples
androidtextchanged

how to save Editable s value in afterTextChanged?


I am going to save the value in one textview after the length of input string is fulfilled. but saved value is empty if the value of textview is used. If Editable s value in afterTextChanged is used, it causes crash.

Some codes as following: number = (EditText) findViewById(R.id.number); final String numberStr = number.getText().toString();

if following afterTextChanged is used, empty value is saved even I already input sth.

  @Override
  public void afterTextChanged(Editable s) {
            if (s.length() == 11) {
                Toast.makeText(MainActivity.this, numberStr , Toast.LENGTH_SHORT).show();
                saveSettingNote(MainActivity.this, "number_save", "number", numberStr);
                number.setText(getSettingNote(MainActivity.this,"number_save", "number"));
            } 
        }

if following code is used, it will cause crash:

   @Override
   public void afterTextChanged(Editable s) {

            if (s.length() == 11) {
                Toast.makeText(MainActivity.this, s.toString(), Toast.LENGTH_SHORT).show();
                saveSettingNote(MainActivity.this, "number_save", "number", s.toString());
                number.setText(getSettingNote(MainActivity.this,"number_save", "number"));
            } 
        }

Saving and getting are based on SharedPreferences, which works well in other situation.

Actually, what I want to implement is saving String after the criteria is fulfilled for input string.

Please help to identify what is wrong in above code or suggest a new to get that function. Thanks a lot in advance.


Solution

  • Your app is crashing because your listener is looping indefinitely with the same value passed in over and over.

    The ugly truth is that you've to unbind your listener, set the value and then bind it again:

    @Override
       public void afterTextChanged(Editable s) {
                if (s.length() == 11) {
                    // unbind your listener
                    editText.addTextChangedListener(null);
    
                    // do your stuff
                    Toast.makeText(MainActivity.this, s.toString(), Toast.LENGTH_SHORT).show();
                    saveSettingNote(MainActivity.this, "number_save", "number", s.toString()); 
                    number.setText(getSettingNote(MainActivity.this,"number_save", "number"));
    
                    // bind your listener again
                    editText.addTextChangedListener(MainActivity.this);
                } 
            }
    

    Using RxBinding you can achieve this in a more elegant way:

    RxTextView.textChanges(editText)
                .map { it.toString() }
                .filter({ it.length == 11 })
                .distinctUntilChanged() // <-- the important part
                .subscribe(
                    { s -> /* do your stuff here */}
                )