Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling different queries in a ListView using a CursorLoader
    primarykey
    data
    text
    <p>I want to reflect data from various tables in a sqlite database at different times in a single ListView.</p> <p>The initial query works fine, which goes like this:</p> <ul> <li>I have a custom CursorAdapter class (<code>AutoCursorAdapter extends CursorAdapter</code>) which can display arbitrary data from a Cursor: <ul> <li><code>AutoCursorAdapter.newView()</code> interrogates the cursor and constructs a view corresponding to the types of data in the columns.</li> <li>AutoCursorAdapter.bindView() takes the data from the cursor and populates the view created in newView for each row of data in the cursor.</li> </ul></li> <li><p>I also implement a loader in the owning Fragment as follows:</p> <pre><code>public class MyFragment extends Fragment implements LoaderManager.LoaderCallbacks&lt;Cursor&gt;{ ... public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ... mListView = ((ListView) view.findViewById(R.id.listview)); mAdapter = new AutoCursorAdapter(getActivity()); mListView.setAdapter(mAdapter); return view; } public void onStart() { super.onStart(); // Prepare the loader. Either re-connect with an existing one, // or start a new one. LoaderManager lm = getLoaderManager(); Bundle args = getArguments(); lm.initLoader(LOADER_ID, args, this); } public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle args) { CursorLoader CursorLoader = new CursorLoader(getActivity()); setupLoader(CursorLoader); //Sets the URI, projection etc. depending on app state return CursorLoader; } public void onLoaderReset(Loader&lt;Cursor&gt; loader) { mAdapter.swapCursor(null); } } </code></pre></li> </ul> <p>So far, so good. The data gets loaded on the initial run. Let's say it pulls and displays data from table A using URI_A.</p> <p><strong>But now the question is</strong>: How do I deal with the case where I want to switch over to displaying data from table B using URI_B in response to a user action? </p> <ul> <li><p>Can I do this with a single Loader instance? </p> <ol> <li>If so how? For instance <code>CursorLoader.setUri()</code> does not trigger a reload.</li> <li>If not, how do I manage multiple loader instances properly?</li> </ol></li> <li><p>Is this an instance where Loaders perhaps aren't the way to go?</p></li> </ul> <p><em>Note: the question is <strong>not</strong> about reflected changes in the underlying DB in table A. This gets updated in the UI correctly already through the NotifyChange mechanism. This question is about changing the nature of the underlying query altogether.</em></p>
    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.
 

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