Note that there are some explanatory texts on larger screens.

plurals
  1. POLoader not loading data when source data changes
    primarykey
    data
    text
    <p>Android Documentation states that LOADERS - They monitor the source of their data and deliver new results when the content changes. I have changed CursorAdapter to work for a SQLite Database. </p> <pre><code>import android.content.ContentResolver; import android.content.Context; import android.database.ContentObserver; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.content.Loader.ForceLoadContentObserver; import android.util.Log; public class SimpleCursorLoader extends AsyncTaskLoader&lt;Cursor&gt; { final ForceLoadContentObserver mObserver; String mTable; String[] mColumns; String mSelection; String[] mSelectionArgs; String mGroupBy; String mHaving; String mOrderBy; String mLimit; Cursor mCursor; SQLiteDatabase mDb; /** * 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 SimpleCursorLoader(Context context) { super(context); mObserver = new ForceLoadContentObserver(); } /** * Creates a fully-specified SimpleCursorLoader. 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 SimpleCursorLoader(Context context,String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit, SQLiteDatabase db) { super(context); mObserver = new ForceLoadContentObserver(); mTable = table; mColumns = columns; //copying array mSelection = selection; mSelectionArgs = selectionArgs; mGroupBy = groupBy; mHaving = having; mOrderBy = orderBy; mLimit = limit; mDb = db; } /* Runs on a worker thread */ @Override public Cursor loadInBackground() { Cursor cursor = mDb.query(mTable, mColumns, mSelection, mSelectionArgs, mGroupBy, mHaving, mOrderBy, mLimit); if (cursor != null) { // Ensure the cursor window is filled cursor.getCount(); Log.d("SimpleCursorLoader","Cursor.getCount()= " + String.valueOf(cursor.getCount())); registerContentObserver(cursor, mObserver); } return cursor; } /** * Registers an observer to get notifications from the content provider * when the cursor needs to be refreshed. */ void registerContentObserver(Cursor cursor, ContentObserver observer) { cursor.registerContentObserver(mObserver); } /* Runs on the UI thread */ @Override public void deliverResult(Cursor cursor) { Log.d("Gaurav","Inside SimpleCursorLoader - deliverResult"); 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 &amp;&amp; oldCursor != cursor &amp;&amp; !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. * * Must be called from the UI thread */ @Override protected void onStartLoading() { Log.d("Gaurav", "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 &amp;&amp; !cursor.isClosed()) { cursor.close(); } } @Override protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); if (mCursor != null &amp;&amp; !mCursor.isClosed()) { mCursor.close(); } mCursor = null; } } </code></pre> <p>The CursorLoader works in a ListFragment with a custom adapter which extends SimpleCursor Adapter and passes FLAG_REGISTER_CONTENT_OBSERVER in constructor but the cursor does changes when underlying data is changed by used response in the list.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload