Infinite Loop addTextChangedListener Android

0

In the Create of my Activity, I have the following

entidadFinanciera.addTextChangedListener(filterTextWatcher);

which does

private TextWatcher filterTextWatcher = new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        // no se necesita
    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
        // no se necesita
    }

    @Override
    public void afterTextChanged(Editable s) {


        filtro(s.toString());

    }
};

Filter:

private void filtro(String s) {

    entidadFinanciera.removeTextChangedListener(filterTextWatcher);
    Spinner spinerBancos = (Spinner) findViewById(R.id.imgpayment);

    int cantidad = bancos.size();
    bancosCopia = bancos ;
    int iterador = 0 ;
    final ArrayList<String> bancosAux = new ArrayList<>();
    while(iterador<cantidad){
        if(bancos.get(iterador).contains(s)){
            bancosAux.add(bancos.get(iterador));
        }
        iterador++;
    }


    final ArrayAdapter<String> dataAdapte = new ArrayAdapter<>(Registry.this, R.layout.support_simple_spinner_dropdown_item, bancosAux);
    dataAdapte.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
    spinerBancos.setAdapter(dataAdapte);
    spinerBancos.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
            Object agencia = dataAdapte.getItem(position);
            String agenciaSeleccionada  = agencia.toString();
            EditText edit = (EditText)findViewById(R.id.txtEntidadFinanciera) ;
            numeroDeEntidad = agenciaSeleccionada.substring(0,3).trim();
            edit.setText(agenciaSeleccionada.substring(4,agenciaSeleccionada.length()));
            bancos = bancosCopia;
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapter) {
            // no se necesita
        }
    });


    entidadFinanciera.addTextChangedListener(filterTextWatcher);

}

In a nutshell, I have an EDITTEXT, where I write the filter and load that to a spinner, and once I have chosen something from that SPINNER to place it in the EDITTEXT, but this one remains in an infinite loop, try to eliminate it at the beginning. listener and put it back to the last but still remains a loop

    
asked by Bruno Sosa Fast Tag 10.01.2018 в 00:44
source

1 answer

2

You are removing the TextWatcher at the beginning of the method but you add it at the end:

private void filtro(String s) {

    entidadFinanciera.removeTextChangedListener(filterTextWatcher);
    //...

    entidadFinanciera.addTextChangedListener(filterTextWatcher);
}

So it's like you do nothing.

Maybe what you need to delete the TextWatcher before editing the EditText text and adding it once edited. This would be in the method setOnItemSelectedListener :

private void filtro(String s) {

    //...
    spinerBancos.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {

            //...
            // eliminamos el Watcher para que no se ejecute
            edit.removeTextChangedListener(filterTextWatcher);
            edit.setText(agenciaSeleccionada.substring(4,agenciaSeleccionada.length()));
            bancos = bancosCopia;

            // le agregamos el watchers para que el usuario pueda buscar mas filtro al escribir
            entidadFinanciera.addTextChangedListener(filterTextWatcher);

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapter) {
            // no se necesita
        }
    });

}

Complete code:

private void filtro(String s) {

Spinner spinerBancos = (Spinner) findViewById(R.id.imgpayment);

int cantidad = bancos.size();
bancosCopia = bancos ;
int iterador = 0 ;
final ArrayList<String> bancosAux = new ArrayList<>();
while(iterador<cantidad){
    if(bancos.get(iterador).contains(s)){
        bancosAux.add(bancos.get(iterador));
    }
    iterador++;
}


final ArrayAdapter<String> dataAdapte = new ArrayAdapter<>(Registry.this, R.layout.support_simple_spinner_dropdown_item, bancosAux);
dataAdapte.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinerBancos.setAdapter(dataAdapte);
spinerBancos.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
        Object agencia = dataAdapte.getItem(position);
        String agenciaSeleccionada  = agencia.toString();
        EditText edit = (EditText)findViewById(R.id.txtEntidadFinanciera);


        numeroDeEntidad = agenciaSeleccionada.substring(0,3).trim();

        // eliminamos el Watcher para que no se ejecute
        edit.removeTextChangedListener(filterTextWatcher);
        edit.setText(agenciaSeleccionada.substring(4,agenciaSeleccionada.length()));
        bancos = bancosCopia;

        // le agregamos el watchers para que el usuario pueda buscar mas filtro al escribir
        entidadFinanciera.addTextChangedListener(filterTextWatcher);

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapter) {
        // no se necesita
    }
});

}
    
answered by 10.01.2018 / 01:24
source