Cursor count returns more values but only adds an item to the list

0

I'm looking at a table of Clients, I'm selecting everything for testing, but what's happening is that by playing the query on the cursor and giving a getCount it returns 4 as expected, a moveToFirst the position goes to 0 , okay, add it right, when giving the moveToNext the position of the cursor goes to 3 ai ends the loop of adding obj in the list, below it follows the Sources: p>

Method that returns the list:

public ArrayList<Cliente> getClienteByCpfCnpj2() {
    Cliente cliente;
    ArrayList<Cliente> lista = new ArrayList<Cliente>();

    cursor = null;
    database = banco.getReadableDatabase();
    cursor = database.query(CriaBanco.TABELA_CLIENTE, null, null, null, null, null, null);
    cursor.moveToFirst();
        do {
            cliente = getClienteById(cursor.getInt(0));
            cliente.setLinha1(cliente.getNome());
            lista.add(cliente);
            cursor.moveToNext();
            int a = cursor.getPosition();
            int b = cursor.getCount();
        } while (cursor.isLast());
    cursor.close();
    return lista;
}

Method getClienteById:

public Cliente getClienteById(int idCliente) {
    Cliente cliente = new Cliente();
    database = banco.getReadableDatabase();
    try {
        cursor = database.rawQuery(
                " SELECT c._Id, c.IdGrupoEmpresa, c.Nome, c.NomeFantasia, c.TipoPessoa, C.CpfCnpj," +
                        "       e._Id ,e.TipoEnd,e.EndCliente, e.ComplEnd,e.Numero,e.Cep, e.IdCliente, e.IdBairro," +
                        "       b._Id, b.Nome," +
                        "       m._Id, m.Nome, m.CodIbge," +
                        "       est._Id, est.Nome, est.Sigla" +
                        " FROM Cliente c" +
                        " LEFT JOIN Endereco e on (e.IdCliente = c._Id)" +
                        " LEFT JOIN Bairro b on (e.IdBairro = b._Id)" +
                        " LEFT JOIN Municipio m on (b.IdMunicipio = m._Id)" +
                        " LEFT JOIN Estado est on (m.IdEstado = est._Id)" +
                        " WHERE c._Id =" + idCliente, null);
        if (cursor != null && cursor.moveToFirst()) {
            cliente.setId(cursor.getInt(0));
            cliente.setIdGrupoEmpresa(cursor.getInt(1));
            cliente.setNome(cursor.getString(2));
            cliente.setNomeFantasia(cursor.getString(3));
            cliente.setTipoPessoa(cursor.getString(4));
            cliente.setCpfCnpj(cursor.getString(5));
            cliente.getEndereco().setId(cursor.getInt(6));
            cliente.getEndereco().setTipoEnd(cursor.getString(7));
            cliente.getEndereco().setEndCliente(cursor.getString(8));
            cliente.getEndereco().setComplEnd(cursor.getString(9));
            cliente.getEndereco().setNumero(cursor.getString(10));
            cliente.getEndereco().setCep(cursor.getString(11));
            cliente.getEndereco().setIdCliente(cursor.getInt(12));
            cliente.getEndereco().setIdBairro(cursor.getInt(13));
            cliente.getEndereco().getBairro().setId(cursor.getInt(14));
            cliente.getEndereco().getBairro().setNome(cursor.getString(15));
            cliente.getEndereco().getBairro().getMunicipio().setId(cursor.getInt(16));
            cliente.getEndereco().getBairro().getMunicipio().setNome(cursor.getString(17));
            cliente.getEndereco().getBairro().getMunicipio().setCodIbge(cursor.getString(18));
            cliente.getEndereco().getBairro().getMunicipio().getEstado().setId(cursor.getInt(19));
            cliente.getEndereco().getBairro().getMunicipio().getEstado().setNome(cursor.getString(20));
            cliente.getEndereco().getBairro().getMunicipio().getEstado().setSigla(cursor.getString(21));

            cliente.setListaTelefone(getListaTelefone(cursor.getInt(0)));
            cliente.setListaEmail(getListaEmail(cursor.getInt(0)));
            cursor.close();
        }
    } catch (Exception e) {
        //
    }
    return cliente;
}

Part where I make the list adaptation in the ListView:

ArrayList<Cliente> listaView = dao.getClienteByCpfCnpj2();
listaClienteCons = (ListView) findViewById(R.id.lstClienteCons);
listaClienteCons.setAdapter(new ClienteConsBaseAdapter(this, listaView));
    
asked by anonymous 09.11.2015 / 15:01

1 answer

0

The condition used in while(cursor.isLast()) returns false on the first run because the cursor is not in the last position.

In order for the test result to work, it must be denied: while(!cursor.isLast()) .

As an alternative to cursor.isLast() I suggest that you use cursor.moveToNext() in the test, in addition to moving the cursor to the next position it will return false when it reaches the end:

cursor = database.query(CriaBanco.TABELA_CLIENTE, null, null, null, null, null, null);
if(cursor != null && cursor.moveToFirst()){
    do{
        cliente = getClienteById(cursor.getInt(0));
        cliente.setLinha1(cliente.getNome());
        lista.add(cliente);
        int a = cursor.getPosition();
        int b = cursor.getCount();
    }while(cursor.moveToNext());
}

It is also advisable to test if the cursor is not null.

    
09.11.2015 / 15:21