Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------ MyCustomLoader.java ------------
- public class MyCustomLoader extends AsyncTaskLoader<PojoWrapper> {
- final ForceLoadContentObserver mObserver;
- Uri mUri;
- String[] mProjection;
- String mSelection;
- String[] mSelectionArgs;
- String mSortOrder;
- PojoWrapper wrapper;
- CancellationSignal mCancellationSignal;
- /* Runs on a worker thread */
- @Override
- public PojoWrapper loadInBackground() {
- synchronized (this) {
- if (isLoadInBackgroundCanceled()) {
- throw new OperationCanceledException();
- }
- mCancellationSignal = new CancellationSignal();
- }
- try {
- Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection,
- mSelectionArgs, mSortOrder, mCancellationSignal);
- if (cursor != null) {
- try {
- // Ensure the cursor window is filled.
- cursor.getCount();
- cursor.registerContentObserver(mObserver);
- } catch (RuntimeException ex) {
- cursor.close();
- throw ex;
- }
- }
- // `CursorLoader` performs following:
- // return cursor;
- // We perform some operation here with `cursor`
- // and return PojoWrapper, that consists of `cursor` and `List<Pojo>`
- return new PojoWrapper(cursor, new ArrayList<Pojo>());
- } finally {
- synchronized (this) {
- mCancellationSignal = null;
- }
- }
- }
- @Override
- public void cancelLoadInBackground() {
- super.cancelLoadInBackground();
- synchronized (this) {
- if (mCancellationSignal != null) {
- mCancellationSignal.cancel();
- }
- }
- }
- /* Runs on the UI thread */
- @Override
- public void deliverResult(PojoWrapper wrapper) {
- Cursor cursor = wrapper.cursor;
- if (isReset()) {
- // An async query came in while the loader is stopped
- if (cursor != null) {
- cursor.close();
- }
- return;
- }
- Cursor oldCursor = this.wrapper.cursor;
- this.wrapper.cursor = cursor;
- if (isStarted()) {
- super.deliverResult(wrapper);
- }
- if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
- oldCursor.close();
- }
- }
- /**
- * Creates an empty unspecified CursorLoader. You must follow this with
- * calls to {@link #setUri(Uri)}, {@link #setSelection(String)}, etc
- * to specify the query to perform.
- */
- public MyCustomLoader(Context context) {
- super(context);
- mObserver = new ForceLoadContentObserver();
- }
- /**
- * Creates a fully-specified CursorLoader. See
- * {@link ContentResolver#query(Uri, String[], String, String[], String)
- * ContentResolver.query()} for documentation on the meaning of the
- * parameters. These will be passed as-is to that call.
- */
- public MyCustomLoader(Context context, Uri uri, String[] projection, String selection,
- String[] selectionArgs, String sortOrder) {
- super(context);
- mObserver = new ForceLoadContentObserver();
- mUri = uri;
- mProjection = projection;
- mSelection = selection;
- mSelectionArgs = selectionArgs;
- mSortOrder = sortOrder;
- }
- /**
- * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks
- * will be called on the UI thread. If a previous load has been completed and is still valid
- * the result may be passed to the callbacks immediately.
- *
- * Must be called from the UI thread
- */
- @Override
- protected void onStartLoading() {
- if (wrapper != null) {
- deliverResult(wrapper);
- }
- if (takeContentChanged() || wrapper == null) {
- forceLoad();
- }
- }
- /**
- * Must be called from the UI thread
- */
- @Override
- protected void onStopLoading() {
- // Attempt to cancel the current load task if possible.
- cancelLoad();
- }
- @Override
- public void onCanceled(PojoWrapper wrapper) {
- Cursor cursor = wrapper.cursor;
- if (cursor != null && !cursor.isClosed()) {
- cursor.close();
- }
- }
- @Override
- protected void onReset() {
- super.onReset();
- // Ensure the loader is stopped
- onStopLoading();
- Cursor cursor = wrapper.cursor;
- if (cursor != null && !cursor.isClosed()) {
- cursor.close();
- }
- wrapper.cursor = null;
- }
- public Uri getUri() {
- return mUri;
- }
- public void setUri(Uri uri) {
- mUri = uri;
- }
- public String[] getProjection() {
- return mProjection;
- }
- public void setProjection(String[] projection) {
- mProjection = projection;
- }
- public String getSelection() {
- return mSelection;
- }
- public void setSelection(String selection) {
- mSelection = selection;
- }
- public String[] getSelectionArgs() {
- return mSelectionArgs;
- }
- public void setSelectionArgs(String[] selectionArgs) {
- mSelectionArgs = selectionArgs;
- }
- public String getSortOrder() {
- return mSortOrder;
- }
- public void setSortOrder(String sortOrder) {
- mSortOrder = sortOrder;
- }
- }
- ------------ PojoWrapper.java ------------
- public class PojoWrapper {
- Cursor cursor;
- List<Pojo> list;
- public PojoWrapper(Cursor cursor, List<Pojo> list) {
- this.cursor = cursor;
- this.list = list;
- }
- }
- ------------ Pojo.java ------------
- public class Pojo {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement