Error TextWatcher () does not work correctly

0

Hi there has been a problem with the TextWatcher class, I have tried to find solutions on the subject in this forum and English but I have not found what I was looking for, I have this piece of code in my method onCreate ():

    textoBuscado = (EditText) findViewById(R.id.textobuscado);


    textoBuscado.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before, int count) {


        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        public void afterTextChanged(Editable s) {

            buscarCadena(textoBuscado.getText().toString());

        }


    });

My layout is composed of two editText: textSearch and textContents.

The string method is a method that performs the following action:

If in my editText'Content text 'there is the letter "r" and I write an "r" in my editText'Search text' that "r" in the'Content text 'will be colored blue.

This works correctly, the problem is that if I delete the letter "r" of textSearch to write another the application freezes stops and I have to force its closure.

How can I solve this problem? What is really happening?

EDIT

The method works correctly because the text is colored and if I execute the method using a button everything works correctly, the problem is that I want to do it with textWatcher.

The searchString method is as follows: textEdit represents the editText "textContent" that I mentioned.

public void buscarCadena(String cadena) {

    String miTexto = textoEditado.getText().toString();

    String textoAuxiliar = miTexto;

    SpannableString ss = new SpannableString(miTexto);

    int guarda = 0;

    while (textoAuxiliar.contains(cadena)) {

        int index = textoAuxiliar.indexOf(cadena);
        int longitud = cadena.length();

        if (guarda == 0) guarda = index;

        ss.setSpan(new ForegroundColorSpan(Color.BLUE), index, index + longitud, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        textoAuxiliar = textoAuxiliar.replaceFirst(cadena, " ");

    }

    textoEditado.setText(ss);
    textoEditado.setSelection(guarda);



}
    
asked by Reductor 24.09.2017 в 20:14
source

1 answer

2

The problem is that when you erase the letter, the search editText is empty and then somewhere in the code search Chain that is generating an error, the changes that can give you solution are as follows.

  • Change within the textWatcher the method that sends the string, and change to the element that reaches you within the implemented interface.

  • Validate the chain that arrives to you, if it arrives to you "" // an empty element, do not invoke the method searchString, you should only clean the chain since the search element is empty (leave the chain all black)

                textoBuscado.addTextChangedListener(new TextWatcher() {
    
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if(!s.toString().equals("")){
                        buscarCadena(s.toString());
    }
                        else {
                       //Limpiar la cadena ya que la busqueda de letras es  vacia.
                       }
                }
    
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
                public void afterTextChanged(Editable s) {
    
                }
            });
    
  • answered by 25.09.2017 в 17:06