Android Multi-thread Attempt in TextWatcher

0

I have an editText in my application which has a TextWatcher implemented in the creation of the class

  

txtBeneficiary = (EditText) findViewById (R.id.txtBeneficiary)   txtBeneficiary.addTextChangedListener (filterTextWatcher);

private TextWatcher filterTextWatcher = new TextWatcher() {


    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        CambiarTexto(s.toString());
    }

};

  private void CambiarTexto(String s) {

        txtBeneficiary.addTextChangedListener(filter);
        String txtIngreso = s ;
        if(contactosCel.contains(txtIngreso)){

            int pos = contactosCel.indexOf(txtIngreso);
            txtBeneficiary.setText(contactos.get(pos));

        }


        txtBeneficiary.addTextChangedListener(filterTextWatcher);
    }

until here all right, the issue is that by writing many letters quickly, you can see that the process becomes quite slow, until you get stuck, doing a little research, I found that the best way is to put it in a secondary thread, so that the principal does not stop, now try the following

 new Thread(new Runnable() {
            public void run() {
                txtBeneficiary.addTextChangedListener(filterTextWatcher);
            }
        }).start();

But it has not taken effect, never use multi thread in any language, so any detail to solve this will be very helpful, thank you already

    
asked by Bruno Sosa Fast Tag 31.10.2017 в 15:37
source

1 answer

2

For each letter you write, you add a TextWatcher to EditText . In the TextWatcher you have the following:

@Override
public void afterTextChanged(Editable s) {
    CambiarTexto(s.toString());
}

What in turn the method CambiarTexto(string) adds 2 TextWatcher s:

private void CambiarTexto(String s) {

    // agregando un TextChangedListener
    txtBeneficiary.addTextChangedListener(filter);

    ///...

    // registrado otro TextChangedListener
    txtBeneficiary.addTextChangedListener(filterTextWatcher);
}

What in turn the listener txtBeneficiary executes the method CambiarTexto(string) and so it is repeated for each letter. Which means that when you write 2 times, you're running 9 TextWatcher ! (And it might be more).

I do not know exactly what you're trying but you're doing it the wrong way.

    
answered by 31.10.2017 / 16:00
source