Skip to main content

Usage CursorLoader without ContentProvider


Android SDK documentation says that startManagingCursor() method is depracated:




This method is deprecated. Use the new CursorLoader class with LoaderManager instead; this is also available on older platforms through the Android compatibility package. This method allows the activity to take care of managing the given Cursor's lifecycle for you based on the activity's lifecycle. That is, when the activity is stopped it will automatically call deactivate() on the given Cursor, and when it is later restarted it will call requery() for you. When the activity is destroyed, all managed Cursors will be closed automatically. If you are targeting HONEYCOMB or later, consider instead using LoaderManager instead, available via getLoaderManager()




So I would like to use CursorLoader . But how can I use it with custom CursorAdapter and without ContentProvider , when I needs URI in constructor of CursorLoader ?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. I created a simple CursorLoader that does not need a content provider:

    import android.content.Context;
    import android.database.Cursor;
    import android.support.v4.content.AsyncTaskLoader;

    /**
    * Used to write apps that run on platforms prior to Android 3.0. When running
    * on Android 3.0 or above, this implementation is still used; it does not try
    * to switch to the framework's implementation. See the framework SDK
    * documentation for a class overview.
    *
    * This was based on the CursorLoader class
    */
    public abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor> {
    private Cursor mCursor;

    public SimpleCursorLoader(Context context) {
    super(context);
    }

    /* Runs on a worker thread */
    @Override
    public abstract Cursor loadInBackground();

    /* Runs on the UI thread */
    @Override
    public void deliverResult(Cursor cursor) {
    if (isReset()) {
    // An async query came in while the loader is stopped
    if (cursor != null) {
    cursor.close();
    }
    return;
    }
    Cursor oldCursor = mCursor;
    mCursor = cursor;

    if (isStarted()) {
    super.deliverResult(cursor);
    }

    if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
    oldCursor.close();
    }
    }

    /**
    * 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.
    * <p/>
    * Must be called from the UI thread
    */
    @Override
    protected void onStartLoading() {
    if (mCursor != null) {
    deliverResult(mCursor);
    }
    if (takeContentChanged() || mCursor == 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(Cursor cursor) {
    if (cursor != null && !cursor.isClosed()) {
    cursor.close();
    }
    }

    @Override
    protected void onReset() {
    super.onReset();

    // Ensure the loader is stopped
    onStopLoading();

    if (mCursor != null && !mCursor.isClosed()) {
    mCursor.close();
    }
    mCursor = null;
    }
    }


    It only needs the AsyncTaskLoader class. Either the one in Android 3.0 or higher, or the one that comes with the compatibility package.

    ReplyDelete
  2. Write your own loader that uses your database class instead of a content provider. The easiest way is just to take the source of the CursorLoader class from the compatibility library, and replace provider queries with queries to your own db helper class.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.