Skip to content

Instantly share code, notes, and snippets.

@smaspe
Last active December 19, 2015 11:39
Show Gist options
  • Save smaspe/5948933 to your computer and use it in GitHub Desktop.
Save smaspe/5948933 to your computer and use it in GitHub Desktop.
public static <T extends Storable> T getById(Context context,
Class<T> clazz, long id) {
try {
T result = clazz.newInstance();
Cursor content = context.getContentResolver().query(
getPath(clazz, id), null, null, null, null);
if (content.moveToFirst()) {
result.loadCursor(content);
}
return result;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
private void loadCursor(Cursor content) {
Field[] fields = SQLHelper.getFields(getClass());
for (Field field : fields) {
TypeHandler handler = TypeHandler.getHandler(field.getType());
try {
field.set(this, handler.fromCursor(field.getName(), content));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment