Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CursorLoaderParser implements LoaderManager.LoaderCallbacks<Cursor> {
- private Context context;
- private LoaderManager lm;
- private Class type;
- private OnLoaderEnd callback;
- private String selection;
- private String[] arguments;
- private String sortOder;
- public CursorLoaderParser(Context context,
- LoaderManager lm,
- Class type,
- OnLoaderEnd callback,
- int id) {
- this.context = context;
- this.lm = lm;
- this.callback = callback;
- this.type = type;
- this.lm.restartLoader(id, null, this);
- }
- public CursorLoaderParser(Context context,
- LoaderManager lm,
- Class type,
- String selection,
- String[] arguments,
- String sortOder,
- OnLoaderEnd callback,
- int id) {
- this.context = context;
- this.lm = lm;
- this.callback = callback;
- this.type = type;
- this.selection = selection;
- this.arguments = arguments;
- this.sortOder = sortOder;
- this.lm.restartLoader(id, null, this);
- }
- public CursorLoaderParser(Context context,
- LoaderManager lm,
- Class type,
- String sortOder,
- OnLoaderEnd callback,
- int id) {
- this.context = context;
- this.lm = lm;
- this.callback = callback;
- this.type = type;
- this.selection = "";
- this.sortOder = sortOder;
- this.lm.restartLoader(id, null, this);
- }
- @Override
- public Loader<Cursor> onCreateLoader(int id, Bundle args) {
- return new CursorLoader(context,
- ContentProvider.createUri(type, null),
- null,
- selection != null ? selection : null,
- arguments != null ? arguments : null,
- sortOder != null ? sortOder : null);
- }
- @Override
- public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
- ArrayList<Model> models = new ArrayList<Model>();
- if (data != null) {
- while (data.moveToNext()) {
- try {
- Model model = (Model)type.newInstance();
- model.loadFromCursor(data);
- models.add(model);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- this.callback.onEnd(models);
- }
- @Override
- public void onLoaderReset(Loader<Cursor> loader) {
- this.callback.onEnd(new ArrayList<Model>());
- }
- public static interface OnLoaderEnd {
- public void onEnd(List<?> items);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment