Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think SwipeToDismissUndoList is not good for cursor-based adapters. Because the adapters rely on changes from content providers (<a href="http://developer.android.com/reference/android/database/Cursor.html#setNotificationUri%28android.content.ContentResolver,%20android.net.Uri%29" rel="nofollow"><code>setNotificationUri()</code></a> or <a href="http://developer.android.com/reference/android/database/Cursor.html#registerContentObserver%28android.database.ContentObserver%29" rel="nofollow"><code>registerContentObserver()</code></a>…) to update UI. You don't know when the data is available or not. That's the problem you're facing.</p> <p>I think there is something like a trick. You can use <a href="http://developer.android.com/reference/android/database/MatrixCursor.html" rel="nofollow"><code>MatrixCursor</code></a>.</p> <ul> <li>In <a href="http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html#onLoadFinished%28android.content.Loader%3cD%3e,%20D%29" rel="nofollow"><code>onLoadFinished(Loader, Cursor)</code></a>, you keep a reference to the cursor returned from content provider. You need to close it manually later.</li> <li>In <code>SwipeDismissList.OnDismissCallback.onDismiss()</code>, create new <code>MatrixCursor</code>, copy all items from the current cursor <em>except</em> the items that are being removed.</li> <li>Set the newly created matrix cursor to the adapter with <a href="http://developer.android.com/reference/android/widget/CursorAdapter.html#swapCursor%28android.database.Cursor%29" rel="nofollow"><code>swapCursor()</code></a> (<em>not</em> <code>changeCursor()</code>). Because <code>swapCursor()</code> doesn't close the old cursor. You need to keep it open so the loader works properly.</li> <li>Now the UI is updated, you make a call to <code>getContentResolver().delete()</code> and actually remove the items that the user wanted to remove. When the content provider finishes deleting data, it notifies the original cursor to reload data.</li> <li><p>Make sure to close the original cursor that you swapped. For example:</p> <pre><code>private Cursor mOrgCursor; @Override public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor data) { if (mOrgCursor != null) mOrgCursor.close(); mOrgCursor = data; mAdapter.changeCursor(mOrgCursor); } @Override public void onLoaderReset(Loader&lt;Cursor&gt; loader) { if (mOrgCursor != null) { mOrgCursor.close(); mOrgCursor = null; } mAdapter.changeCursor(null); } </code></pre></li> <li>Don't worry about the matrix cursor, <code>changeCursor()</code> will close it.</li> </ul>
 

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