Error in the comparison between an EditText and a String

1

I'm trying to compare the text entered in an EditText with an item in my ArrayList, but the condition is never true.

int aux=-1;

for (int i = 0; i < lista2.size(); i++) {
    if (edContato.getText().equals(lista2.get(i))){
        aux = i;
        break;
    }
}

if (rbExcluir.isChecked()) {
    if(aux>=0){
        principal();
        lista2.remove(aux);
        aplicarLista();
    }
    else {
        dialogo.setTitle("Erro!");
        dialogo.setMessage("Contato não encontrado!");
        dialogo.setNeutralButton("OK", null);
        dialogo.show();
        excluir_alterar();
    }

I'm a beginner in programming for Android and as far as I know, in Java this comparison would work. Am I letting anything happen? Does this situation require any other method of comparison?

    
asked by anonymous 06.05.2017 / 23:19

1 answer

3

You're comparing a Editable with a String.

The object returned by EditText # getText () is of type Editable.

To get the string in EditText you must use the% method of Editable%.

Instead of

edContato.getText().equals(lista2.get(i))

use

edContato.getText().toString().equals(lista2.get(i))
    
06.05.2017 / 23:43