Please teach me a simple way to repeat Android Cursor.

Asked 1 years ago, Updated 1 years ago, 114 views

I need a lot of code to check the results of the database over and over again.

Cursor cursor = db.rawQuery(...);
cursor.moveToFirst();
while (cursor.isAfterLast() == false) 
{
    ...
    cursor.moveToNext();
}

Cursor cursor = db.rawQuery(...);
for (boolean hasItem = cursor.moveToFirst(); 
     hasItem; 
     hasItem = cursor.moveToNext()) {
    ...
}

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

All of these codes are too long and call the Cursor method several times, so is there a simpler code?

android cursor

2022-09-22 15:29

1 Answers

while (cursor.moveToNext()) {
    ...
}
cursor.close();

You can do it like this. The cursor starts before the first row and moves to the first row when you call moveToNext. Returns false if the first row is empty or the last row is reached.


2022-09-22 15:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.