While() loop validation bug?

Asked

Viewed 94 times

1

I’m trying to set the total of some values I capture in the Sqlite database, so far so good because the values passed in my Arrayadpter is set on the screen without major problems. The problem is when the loop ends with the cursor returned null, it stops right there and does not execute the rest of the code as if it were stuck in an infinite loop, but I think this is not because my application does not lock and continues to perform other functions like clicks, then due to this at the end of the code in the conditional "if" and "Else" simply do not run and ai does not have the total value set.

Any idea why this is happening?

Note: I tried to change the conditional of the loop to "whilhe(x < 0) { x++ }" and it worked, the problem seems to be in the return of the cursor.

cursor.moveToFirst();

        if ( cursor.getCount() > 0 ) {

            while ( cursor != null ) {

                Date dataInicial;
                SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
                dataInicial = sdf1.parse(cursor.getString(indiceColunaData));

                if (dataInicial.after(ACS.dataFiltro())) {
                    infocard.add("Numero da nota " + cursor.getString(indiceColunaId) + "\n" +
                            "Comprador: " + cursor.getString(indiceColunaPessoa) + "\n" +
                            "Produto: " + cursor.getString(indiceColunaProduto) + "\n" +
                            "Valor: " + cursor.getString(indiceColunaValor) + "R$\n" +
                            "Data da compra: " + cursor.getString(indiceColunaData) + "\n" +
                            "Data de registro: " + cursor.getString(indiceColunaDataAdd).replaceAll(" ", " as ")
                    );

                    soma += Double.parseDouble(cursor.getString(indiceColunaValor));

                }

                cursor.moveToNext();

            }

        }


        cursor.close();
        bancoDados.close();

        DecimalFormat df = new DecimalFormat("#.00");
        String string = df.format(soma);

        StringBuilder SB = new StringBuilder(string);

        if ( soma > 0 ) {
            if (string.length() >= 7 && string.length() <= 9) {
                total.setText("Total: " + SB.insert(string.length() - 6, ".").deleteCharAt(string.length() - 2).insert(string.length() - 2, ",") + " R$");
            } else if (string.length() < 4) {
                total.setText("Total: " + SB.insert(string.length() - 3, "0") + " R$");
            } else {
                total.setText("Total: " + SB.deleteCharAt(string.length() - 3).insert(string.length() - 3, ",") + " R$");
            }
        }else {
            total.setText("Total: 0,00 R$");
        }

1 answer

0


Try to go through the Cursor thus:

Cursor c = db.rawQuery("select...");
c.moveToFirst();

for(int i = 0; i < c.getCount(); i++){
    String valor = c.getString(c.getColumnIndex("Nome da coluna"));
    //Seu código...
    c.moveToNext();

}

c.close();

That way you should have no more problems!

Browser other questions tagged

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