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);
}