Created
November 15, 2016 23:46
-
-
Save keyboardr/10c057bb657627005dcefe4bbe2f3a98 to your computer and use it in GitHub Desktop.
Cursor example
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.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