Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using adapter.swapCursor(cursor) is correct so you're almost there in answering your own question.</p> <p>Your first piece of code doesn't work because when you call fillData() after your database update, you simply call adapter.notifyDataSetChanged() and the dataset hasn't actually changed because the cursor is the same. A cursor is a reference to rows from your database and updating the underlying database doesn't refresh the cursor. Your second piece of code does refresh the cursor and swaps the new one in to the adapter (which also triggers an update to the view it is bound to).</p> <p>The more common way to code this is:</p> <p>Add this interface to your activity:</p> <pre><code>public class MyActivity extends Activity implementsLoaderManager.LoaderCallbacks&lt;Cursor&gt; </code></pre> <p>In onCreate, set up the adapter (note that the cursor is null at this point):</p> <pre><code>String[] from = new String[] { TodoTable.COLUMN_SUMMARY, TodoTable.COLUMN_ID}; int[] to = new int[] { R.id.label }; adapter = new SimpleCursorAdapter(this, R.layout.todo_row, null, from, to, 0); //Note that the cursor is null lw.setAdapter(adapter); </code></pre> <p>Initiate the loader:</p> <pre><code>getLoaderManager().initLoader(0, null, this); </code></pre> <p>This calls onCreateLoader in a background thread (so if your query is long running it won't block the UI thread). When it finishes, onLoadFinished is called on the UI thread where you can swap in the new cursor.</p> <p>After you do a delete or update, restart the loader:</p> <pre><code>getLoaderManager().restartLoader(0, null, this); </code></pre> <p>This calls onLoaderReset which removes the existing cursor from the adapter and then calls onCreateLoader again to swap in a new one.</p> <p>Finally add these methods:</p> <pre><code>public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle args) { String[] from = new String[] { TodoTable.COLUMN_SUMMARY, TodoTable.COLUMN_ID}; String where = TodoTable.COLUMN_DELETED + " = ?"; Loader&lt;Cursor&gt; loader = new CursorLoader(this, TodoContentProvider.CONTENT_URI, from, where, new String[] {"0"}, null); return loader; } public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor cursor) { adapter.swapCursor(cursor); } public void onLoaderReset(Loader&lt;Cursor&gt; loader) { adapter.swapCursor(null); } </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.
    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.
    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