Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it necessary to implement both LoaderCallbacks and OnLoadCompleteListener to get notifications of changes in a ContentProvider?
    primarykey
    data
    text
    <p>I have an application which is using <code>Loader</code>s to get at a database which is also being edited by an <code>IntentService</code>. I receive data from the <code>Loader</code> through a <code>LoaderCallbacks</code> implementation, which is working fine.</p> <p>I am also using <code>ContentResolver#notifyChange(Uri, ContentObserver)</code> to trigger reload. However, this only works when I call <code>Cursor#setNotificationUri(Uri)</code> beforehand.</p> <p>I can find no reference to the latter method in any documentation and it seems in fact this may be causing crashes: see also</p> <p><a href="https://stackoverflow.com/questions/14956608/illegalstateexception-attempt-to-re-open-an-already-closed-object-in-simplecur">IllegalStateException &quot;attempt to re-open an already-closed object&quot; in SimpleCursorAdapter from ContentProvider</a></p> <p>However, without this call on the <code>Cursor</code> the <code>LoaderCallbacks#onLoadFinished(Loader&lt;Cursor&gt;, Cursor)</code> is only hit after the initial load, and not after the notification. Do I <em>also</em> need to implement an <code>OnLoadCompleteListener</code> to do, well, exactly the same thing?</p> <p><code>ContentProvider</code> query method:</p> <pre><code>class MyContentProvider extends ContentProvider { //... @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = mOpenHelper.getReadableDatabase(); Cursor query = db.query(getTableName(uri), projection, selection, selectionArgs, null, null, sortOrder); query.setNotificationUri(getContext().getContentResolver(), uri); return query; } //... } </code></pre> <p>Typical <code>LoaderCallbacks</code>:</p> <pre><code>LoaderCallbacks&lt;Cursor&gt; mCallbacks = new LoaderCallbacks&lt;Cursor&gt;() { @Override public void onLoaderReset(Loader&lt;Cursor&gt; loader) { mArticleAdapter.swapCursor(null); } @Override public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor cursor) { if(cursor.isClosed()) { Log.d(TAG, "CURSOR RETURNED CLOSED"); Activity activity = getActivity(); if(activity!=null) { activity.getLoaderManager().restartLoader(mFragmentId, null, mCallbacks); } return; } mArticleAdapter.swapCursor(cursor); } @Override public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle args) { triggerArticleFeed(); CursorLoader cursorLoader = null; if(id == mFragmentId) { cursorLoader = new CursorLoader(getActivity(), MyContentProvider.ARTICLES_URI, null, ArticlesContentHelper.ARTICLES_WHERE, ArticlesContentHelper.ARTICLES_WHEREARGS, null); } return(cursorLoader); } }; </code></pre>
    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.
 

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