What's the best way to iterate an Android Cursor?

ID : 10328

viewed : 24

Tags : androidandroid-cursorandroid

Top 5 Answer for What's the best way to iterate an Android Cursor?

vote vote


The simplest way is this:

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

The cursor starts before the first result row, so on the first iteration this moves to the first result if it exists. If the cursor is empty, or the last row has already been processed, then the loop exits neatly.

Of course, don't forget to close the cursor once you're done with it, preferably in a finally clause.

Cursor cursor = db.rawQuery(...); try {     while (cursor.moveToNext()) {         ...     } } finally {     cursor.close(); } 

If you target API 19+, you can use try-with-resources.

try (Cursor cursor = db.rawQuery(...)) {     while (cursor.moveToNext()) {         ...     } } 
vote vote


The best looking way I've found to go through a cursor is the following:

Cursor cursor; ... //fill the cursor here  for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {     // do what you need with the cursor here } 

Don't forget to close the cursor afterwards

EDIT: The given solution is great if you ever need to iterate a cursor that you are not responsible of. A good example would be, if you are taking a cursor as argument in a method, and you need to scan the cursor for a given value, without having to worry about the cursor's current position.

vote vote


I'd just like to point out a third alternative which also works if the cursor is not at the start position:

if (cursor.moveToFirst()) {     do {         // do what you need with the cursor here     } while (cursor.moveToNext()); } 
vote vote


Below could be the better way:

if (cursor.moveToFirst()) {    while (!cursor.isAfterLast()) {          //your code to implement          cursor.moveToNext();     } } cursor.close(); 

The above code would insure that it would go through entire iteration and won't escape first and last iteration.

vote vote


How about using foreach loop:

Cursor cursor; for (Cursor c : CursorUtils.iterate(cursor)) {     //c.doSth() } 

However my version of CursorUtils should be less ugly, but it automatically closes the cursor:

public class CursorUtils { public static Iterable<Cursor> iterate(Cursor cursor) {     return new IterableWithObject<Cursor>(cursor) {         @Override         public Iterator<Cursor> iterator() {             return new IteratorWithObject<Cursor>(t) {                 @Override                 public boolean hasNext() {                     t.moveToNext();                     if (t.isAfterLast()) {                         t.close();                         return false;                     }                     return true;                 }                 @Override                 public Cursor next() {                     return t;                 }                 @Override                 public void remove() {                     throw new UnsupportedOperationException("CursorUtils : remove : ");                 }                 @Override                 protected void onCreate() {                     t.moveToPosition(-1);                 }             };         }     }; }  private static abstract class IteratorWithObject<T> implements Iterator<T> {     protected T t;     public IteratorWithObject(T t) {         this.t = t;         this.onCreate();     }     protected abstract void onCreate(); }  private static abstract class IterableWithObject<T> implements Iterable<T> {     protected T t;     public IterableWithObject(T t) {         this.t = t;     } } } 

Top 3 video Explaining What's the best way to iterate an Android Cursor?