Skip to content

Instantly share code, notes, and snippets.

@keyboardr
Created November 15, 2016 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyboardr/10c057bb657627005dcefe4bbe2f3a98 to your computer and use it in GitHub Desktop.
Save keyboardr/10c057bb657627005dcefe4bbe2f3a98 to your computer and use it in GitHub Desktop.
Cursor example
import android.support.annotation.WorkerThread;
import android.support.annotation.Nullable;
import android.database.Cursor;
public class CursorReaderExample {
// Example columns, would likely be in a different file (e.g. a contract class).
private static String COLUMN_FOO = "foo";
private static String COLUMN_BAR = "bar";
@WorkerThread
public static Result readCursor(@Nullable Cursor cursor) {
Result result = null;
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
int fooColumn = cursor.getColumnIndex(COLUMN_FOO);
int barColumn = cursor.getColumnIndex(COLUMN_BAR);
do {
String foo = cursor.getString(fooColumn);
long bar = cursor.getLong(barColumn);
result = Result.fromFooBar(foo, bar);
} while (cursor.moveToNext());
}
} finally {
cursor.close();
}
}
return result;
}
/**
* Just here as an example result class
*/
private static class Result {
public static Result fromFooBar(String foo, long bar) {
return new Result(foo, bar);
}
private final String foo;
private final long bar;
private Result(String foo, long bar) {
this.foo = foo;
this.bar = bar;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment