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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex