0

I used a While Loop to add the values of an SQL table into an ArrayList using the Cursor class, but at the moment of adding all the values, the While Loop stop giving values but, at the same time, it doesn't continue with the next statements. I used the condition that: if inside the While Loop the Cursor has null value, it needs to stop (c != null), it stops but doesn't continue. I used Log.i to see if I receive values after the While Loop, but I don't get any value. Here is the code I am using:

static ArrayList<String> notes = new ArrayList<>();
static ArrayAdapter arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView listView = (ListView) findViewById(R.id.listView);

    try{
        SQLiteDatabase sqLiteDatabase = this.openOrCreateDatabase("Notes",MODE_PRIVATE, null);
        sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS notes (name VARCHAR)");
        sqLiteDatabase.execSQL("INSERT INTO notes (name) VALUES ('LUIS GA')");
        sqLiteDatabase.execSQL("INSERT INTO notes (name) VALUES ('LUIS')");
        sqLiteDatabase.execSQL("INSERT INTO notes (name) VALUES ('GA')");

        Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM notes", null);
        int nameIndex = c.getColumnIndex("name");
        notes.add("example note");
        c.moveToFirst();
        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, notes);
        listView.setAdapter(arrayAdapter);
        int i = 0;
        Log.i("Bucefalo", Integer.toString(i));

        while(c != null){
            Log.i("Dato",c.getString(nameIndex));
            notes.add(c.getString(nameIndex));
            c.moveToNext();
            i++;
            Log.i("Luis", Integer.toString(i));
        };
        i = 2;
        Log.i("Napoleon", Integer.toString(i));
        String Hercules = "Hercules";
        Log.i("Hercules",Hercules);
    }
    catch (Exception e){

        e.printStackTrace();

    }

The values that I receive in the Logcat using Log.i, are "Bucefalo" and "Luis" (the one inside the While Loop), but I don't receive the value "Napoleon" and "Hercules", Why? How I can fix this? Thank you

4 Answers4

2

This is the best way to iterate through the cursor

Cursor cursor = db.rawQuery(...);
try {
        while (cursor.moveToNext()) {
        ...
    }
} finally {
    cursor.close();
}
android_griezmann
  • 3,757
  • 4
  • 16
  • 43
  • You would have my vote if you explain why this is the best way. Don't throw a code at his face like that. – AxelH Oct 12 '16 at 07:15
2

Aside the wrong usage of the cursor, this code

while(c != null){
  ...
  c.moveToNext();
}

should actually never stop looping.

You see: you have a local reference c there. It is not null. Calling a method on a reference absolutely can not turn the reference itself to be null. Only an assignment like c = null would you get there.

In other words: your claims about observed behavior are not supported by the code you are posting.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

moveToNext returns a boolean value, but at the same time it goes ahead and moves the cursor.

Use like this,

Cursor c = sqLiteDatabase.rawQuery(....);
while (c.moveToNext()) {

}

or

do {

} while (c.moveToNext());
Kushan
  • 10,657
  • 4
  • 37
  • 41
0

There is a syntax error in your while Loop.it should be below

while (expression)
{
 statemets(s)
}

and moreover you are using cursor badly.

you can use

cur.moveToFirst();
    while (cur.isAfterLast() == false) {
        .............
        ............
        cur.moveToNext();
    }
    cur.close();

or
Cursor cursor = db.rawQuery(...); if (cursor.moveToFirst()) { do { ...
} while (cursor.moveToNext()); }
or

Cursor cursor = db.rawQuery(...);
 try {
while (cursor.moveToNext()) {
    ...
}
} finally {
cursor.close();
}
Rajesh.k
  • 2,327
  • 1
  • 16
  • 19