What is the best way to repeat Cursor on Android?

Asked 2 years ago, Updated 2 years ago, 94 views

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

As you work through the code, you see a lot of code that processes each row, moves to the next row, and repeats these divi queries All of the codes above look so messy and I think I'm calling the Cursor method too much, is there a simpler way?

android cursor

2022-09-21 19:41

1 Answers

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

Since the cursor starts with the first row, it will repeat well if the first row of the cursor exists. If the cursor is empty or the last row, the repeat statement ends.


2022-09-21 19:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.