Cursor count returns more values but only adds one item in the list

Asked

Viewed 68 times

0

I am consulting a table of Clients, I am selecting all test affine, but what is happening is that when playing the query on the cursor and giving a getCount it returns 4 as expected, but when giving a moveToFirst the position goes to 0, all right, add it right, when giving moveToNext the cursor position goes to 3 ai ends the obj addition loop in the list, below follows the Fonts:

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;
}

Metodo 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 adapt the list in Listview:

ArrayList<Cliente> listaView = dao.getClienteByCpfCnpj2();
listaClienteCons = (ListView) findViewById(R.id.lstClienteCons);
listaClienteCons.setAdapter(new ClienteConsBaseAdapter(this, listaView));
  • These two methods of yours are in the same class?

1 answer

0

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

For the test result to work it must be denied: while(!cursor.isLast()).

As an alternative to cursor.isLast() I suggest you use cursor.moveToNext() in the test, it will not only move the cursor to the next position return false when it comes down to it:

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.

  • Hello, good day ramaral, I did as you suggested, but the same error persists, in moveToNext it is going from position 0 to 3

  • I don’t understand why. Unless int b = cursor.getCount(); move the cursor to the end. Delete that line.

  • I already eliminated, I had put only to debug and see what could be, the strange thing is that if I use another table works perfectly, but I have recreated the bank and persists the error, I thought to be the parameters but the constants are all OK.

  • I don’t know what to say. The shape of do/while which I posted is the most usual way to scroll through a cursor. So the problem is not in it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.