ppamorim

Untitled

Aug 15th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. public class CursorLoaderParser implements LoaderManager.LoaderCallbacks<Cursor> {
  2.  
  3.     private Context context;
  4.     private LoaderManager lm;
  5.     private Class type;
  6.     private OnLoaderEnd callback;
  7.  
  8.     private String selection;
  9.     private String[] arguments;
  10.     private String sortOder;
  11.  
  12.     public CursorLoaderParser(Context context,
  13.                               LoaderManager lm,
  14.                               Class type,
  15.                               OnLoaderEnd callback,
  16.                               int id) {
  17.  
  18.         this.context = context;
  19.         this.lm = lm;
  20.         this.callback = callback;
  21.         this.type = type;
  22.         this.lm.restartLoader(id, null, this);
  23.     }
  24.  
  25.     public CursorLoaderParser(Context context,
  26.                               LoaderManager lm,
  27.                               Class type,
  28.                               String selection,
  29.                               String[] arguments,
  30.                               String sortOder,
  31.                               OnLoaderEnd callback,
  32.                               int id) {
  33.  
  34.         this.context = context;
  35.         this.lm = lm;
  36.         this.callback = callback;
  37.         this.type = type;
  38.         this.selection = selection;
  39.         this.arguments = arguments;
  40.         this.sortOder = sortOder;
  41.         this.lm.restartLoader(id, null, this);
  42.     }
  43.  
  44.     public CursorLoaderParser(Context context,
  45.                               LoaderManager lm,
  46.                               Class type,
  47.                               String sortOder,
  48.                               OnLoaderEnd callback,
  49.                               int id) {
  50.  
  51.         this.context = context;
  52.         this.lm = lm;
  53.         this.callback = callback;
  54.         this.type = type;
  55.         this.selection = "";
  56.         this.sortOder = sortOder;
  57.         this.lm.restartLoader(id, null, this);
  58.     }
  59.  
  60.     @Override
  61.     public Loader<Cursor> onCreateLoader(int id, Bundle args) {
  62.         return new CursorLoader(context,
  63.                 ContentProvider.createUri(type, null),
  64.                 null,
  65.                 selection != null ? selection : null,
  66.                 arguments != null ? arguments : null,
  67.                 sortOder != null ? sortOder : null);
  68.     }
  69.  
  70.     @Override
  71.     public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
  72.  
  73.         ArrayList<Model> models = new ArrayList<Model>();
  74.  
  75.         if (data != null) {
  76.  
  77.             while (data.moveToNext()) {
  78.  
  79.                 try {
  80.  
  81.                     Model model = (Model)type.newInstance();
  82.                     model.loadFromCursor(data);
  83.  
  84.                     models.add(model);
  85.  
  86.                 } catch (Exception e) {
  87.                     e.printStackTrace();
  88.                 }
  89.             }
  90.         }
  91.  
  92.         this.callback.onEnd(models);
  93.     }
  94.  
  95.     @Override
  96.     public void onLoaderReset(Loader<Cursor> loader) {
  97.         this.callback.onEnd(new ArrayList<Model>());
  98.     }
  99.  
  100.     public static interface OnLoaderEnd {
  101.         public void onEnd(List<?> items);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment