0

I have got a database of countries and international organizations. Each country can be a member of a number of the organizations and each organization has at least one member (in other words , many-to-many). I use the well-known LoaderManager framework for displaying. But after I delete an item (some items), all the fragments show the deleted items. I've tried to use this solution from the onContextItemSelected of MyFragment extends SherlockListFragment class but nothing has happened.

SOLVED.

I had a lot of fun trying to find out the cause. I found it at last. If I use a URI Uris.MY_URI as a parameter of the CursorLoader constructor, I will have to use the same URI when deleting. I am sure that there is a more elegant solution but this one works. And I don't even need to call the restartLoader method.

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle loaderExtras) {
    // Some code
    return new CursorLoader(getActivity(), Uris.MY_URI, mProjection, selection, selectionArgs, sortOrder);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    String selection = Columns._ID + "=?";
    String selectionArgs[] = new String[] {
            Long.toString(id)
    };
    long rows = getActivity().getContentResolver().delete(Uris.MY_URI, selection, selectionArgs);

    super.onListItemClick(l, v, position, id);
}

Don't forget about cursor.setNotificationUri(getContext().getContentResolver(), uri); before you return a cursor in the query method and about

if (result > 0) {
        // result is the number of deleted items
        getContext().getContentResolver().notifyChange(uri, null);
}

in the delete method.

Community
  • 1
  • 1
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138
  • Can you add your `LoaderManager.LoaderCallbacks#onCreateLoader(...)` code? – James McCracken May 29 '13 at 13:24
  • @JamesMcCracken, I corrected my question. There was a wrong GitHub link and I added the code to my question. – Maksim Dmitriev May 29 '13 at 13:38
  • I don't see anything obviously wrong with this. Have you stepped through to see that the items are actually being deleted from the content provider? – James McCracken May 29 '13 at 14:46
  • @JamesMcCracken, data is actually deleted from the database. The cause of the bug is the following: if each fragment has its own data, the bug will show and if all the fragments have the same data, the `ListView` will be refreshed even if I don't call `adapter.notifyDataSetChanged()` or `getLoaderManager().restartLoader(LOADER_ID, null, this)` – Maksim Dmitriev May 30 '13 at 09:14

0 Answers0