Filter query with java android sqlite

2
public ArrayList<Contato> filtrar(String filtro) {
    ArrayList<Contato> contatoArray = new ArrayList<>();

    //Consulta que traz todos dados de todas colunas
    Cursor cursor = database.query(BaseDAO.TABELA_AGENDA, BaseDAO.COLUNAS_TABELA_AGENDA, null, null, null, null, BaseDAO.CONTATO_NOME);

    cursor.moveToFirst();
    while(!cursor.isAfterLast()) {

        Contato c = new Contato();

        if (cursor.getString(1).equalsIgnoreCase(filtro) || cursor.getString(3).equalsIgnoreCase(filtro)) {
            c.setId(cursor.getInt(0));
            c.setNome(cursor.getString(1));
            c.setTelefone(cursor.getString(2));
            c.setEmpresa(cursor.getString(3));
            c.setBrunko(cursor.getInt(4) == 1);
            c.setCarter(cursor.getInt(5) == 1);
            c.setFortmetal(cursor.getInt(6) == 1);
            c.setNdflex(cursor.getInt(7) == 1);
            c.setNotus(cursor.getInt(8) == 1);
            c.setMerco(cursor.getInt(9) == 1);
            c.setMetan(cursor.getInt(10) == 1);
            c.setRiosulense(cursor.getInt(11) == 1);
            c.setYming(cursor.getInt(12) == 1);
            c.setMesContato(cursor.getInt(13));
            c.setAnoContato(cursor.getString(14));

            contatoArray.add(c);
        }
        cursor.moveToNext();
    }
    cursor.close();
    return contatoArray;
}

My code seems to be correct with Log.d() I got the values of filtro , cursor.getString(1) and cursor.getString(3) which are the same, for example "Test Name", "Test Name" and "Test Company" but they do not enter if() what is wrong?

    
asked by anonymous 18.09.2017 / 15:54

2 answers

2

Log I noticed that the variables registered were gained a space at the end of them so a variable "x" for example was "x", so it did not work if()

    
25.09.2017 / 17:13
1

You should put the filter in the query and then remove the if condition. Something like:

Cursor cursor = database.query(BaseDAO.TABELA_AGENDA, BaseDAO.COLUNAS_TABELA_AGENDA, "coluna1 = ? or coluna3 = ?", new String[]{filtro, filtro}, null, null, BaseDAO.CONTATO_NOME);
    
19.09.2017 / 16:36