Created
May 19, 2015 12:03
-
-
Save emdete/86f647bbda89922d1670 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.database.Cursor; | |
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
public class CursorI implements Iterable<Cursor> { | |
Cursor cursor; | |
class IteratorI implements Iterator<Cursor> { | |
Cursor cursor; | |
IteratorI(Cursor cursor) { | |
this.cursor = cursor; | |
} | |
public boolean hasNext() { | |
if (cursor == null) | |
return false; | |
boolean ret = cursor.moveToNext(); | |
if (!ret) { | |
cursor.close(); | |
cursor = null; | |
} | |
return ret; | |
} | |
public Cursor next() { | |
return cursor; | |
} | |
public void remove() { | |
throw new UnsupportedOperationException("can't remove from cursors"); | |
} | |
} | |
public CursorI(Cursor cursor) { | |
this.cursor = cursor; | |
} | |
public IteratorI iterator() { | |
IteratorI ret = new IteratorI(cursor); | |
cursor = null; // make sure it's used once only | |
return ret; | |
} | |
/* | |
* short sample on how to use the above code. the foreach will work, if a | |
* cursor is wrapped into a CursorI as shown below. | |
*/ | |
static void test(Cursor cursor) { | |
for (Cursor o: new CursorI(cursor)) { | |
; // do something with the row | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment