Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If anyone finds themselves in a similar situation, here's what I've done:</p> <ul> <li>Created a class which implements <code>LoaderCallbacks</code> and handles <em>all</em> the queries you'll need.</li> <li>Supply this with a <code>Context</code> and the <code>Adapter</code> in question.</li> <li>Create unique IDs for each query you'll use (if you use a <code>UriMatcher</code>, might as well use the same ones)</li> <li>Make a convenience method which transfers queries into the bundle required for the <code>LoaderCallbacks</code></li> <li>That's pretty much it :) I put some of my code below to show exactly what I did</li> </ul> <p>In my <code>GlobalCallbacks</code> class:</p> <pre><code>public static final String PROJECTION = "projection"; public static final String SELECTION = "select"; public static final String SELECTARGS = "sargs"; public static final String SORT = "sort"; Context mContext; SimpleCursorAdapter mAdapter; public GlobalCallbacks(Context context, SimpleCursorAdapter adapter) { mContext = context; mAdapter = adapter; } @Override public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle args) { Uri contentUri = AbsProvider.customIntMatch(id); if (contentUri != null) { return new CursorLoader(mContext, contentUri, args.getStringArray(PROJECTION), args.getString(SELECTION), args.getStringArray(SELECTARGS), args.getString(SORT)); } else return null; } @Override public void onLoadFinished(Loader&lt;Cursor&gt; arg0, Cursor arg1) { mAdapter.swapCursor(arg1); } @Override public void onLoaderReset(Loader&lt;Cursor&gt; arg0) { mAdapter.swapCursor(null); } </code></pre> <p>And when I wanted to use a <code>CursorLoader</code> (<code>Helper.bundleArgs()</code> is the convenience bundling method):</p> <pre><code>scAdapt = new Adapters.NewIndexedAdapter(mHost, getMenuType(), null, new String[] { "name" }, new int[] { android.R.id.text1 }); getLoaderManager().initLoader( GlobalCallbacks.GROUP, Helper.bundleArgs(new String[] { "_id", "name" }), new GlobalCallbacks(mHost, scAdapt)); setListAdapter(scAdapt); </code></pre> <p>And in Helper:</p> <pre><code>public static Bundle bundleArgs(String[] projection, String selection, String[] selectionArgs) { Bundle b = new Bundle(); b.putStringArray(GlobalCallbacks.PROJECTION, projection); b.putString(GlobalCallbacks.SELECTION, selection); b.putStringArray(GlobalCallbacks.SELECTARGS, selectionArgs); return b; } </code></pre> <p>Hope this helps someone else :)</p> <p><strong>EDIT</strong></p> <p>To explain more thoroughly:</p> <ul> <li>First, an adapter with a null <code>Cursor</code> is initialised. We don't supply it with a <code>Cursor</code> because <code>GlobalCallbacks</code> will give the adapter the correct <code>Cursor</code> in <code>onLoadFinished(..)</code></li> <li>Next, we tell <code>LoaderManager</code> we want to initialise a new <code>CursorLoader</code>. We supply a new <code>GlobalCallbacks</code> instance (which implements <code>Loader.Callbacks</code>) which will then monitor the loading of the cursor. We have to supply it with the adapter too, so it can swap in the new <code>Cursor</code> once its done loading. At some point, the <code>LoaderManager</code> (which is built into the OS) will call <code>onCreateLoader(..)</code> of <code>GlobalCallbacks</code> and start asynchronously loading data</li> <li><code>Helper.bundleArgs(..)</code> just puts arguments for the query into a <code>Bundle</code> (e.g. columns projection, sort order, WHERE clause)</li> <li>Then we set the <code>Fragment</code>'s <code>ListAdapter</code>. The cursor will still be null at this point, so it will show a loading sign or empty message until <code>onLoadFinished()</code> is called</li> </ul>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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