I made the delete posibility from cursor , the function works , but CursorAdapter doesnt update on notifyDataSetChanged(), any one has any idea ??? The only way at the moment to see the changes is to exit app and run again
public class DiaryFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemClickListener {
private GridView mGridView;
private View mEmptyViews;
private TextView mAddNewDiaryEntry;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_diary, container, false);
setHasOptionsMenu(true);
getLoaderManager().initLoader(0, null, this);
mGridView = (GridView) view.findViewById(R.id.gridview);
mGridView.setAdapter(new DiaryAdapter(getActivity()));
mGridView.setOnItemClickListener(this);
return view;
}
@Override
public void onResume() {
super.onResume();
GALogEvent.logScreen(getActivity(), "Diary");
getLoaderManager().restartLoader(0, null, this);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.diary, menu);
}
private CursorAdapter getAdapter() {
return ((CursorAdapter) mGridView.getAdapter());
}
public static Fragment newInstance() {
return new DiaryFragment();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(),ContentProvider. URI, null, null, null, ContentProvider.DIARY_DATE + " DESC");
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
getAdapter().swapCursor(data);
if (data != null && data.getCount() > 0) {
mGridView.setVisibility(View.VISIBLE);
mEmptyViews.setVisibility(View.GONE);
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
getAdapter().swapCursor(null);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
Cursor c = (Cursor) parent.getAdapter().getItem(position);
int diaryEntryId = c.getInt(c.getColumnIndex(ContentProvider.ID));
Intent intent = new Intent(getActivity(), AddDiaryEntryActivity.class);
boolean didSmoke = c.getInt(c.getColumnIndex(ContentProvider.DIARY_DID_SMOKE)) != 0;
intent.putExtra(Constants.DIARY_UPDATE_STATUS, didSmoke);
intent.putExtra(Constants.DIARY_ENTRY, diaryEntryId);
startActivity(intent);
}
private class DiaryAdapter extends CursorAdapter {
private final LayoutInflater mLayoutInflater;
public DiaryAdapter(Context context) {
super(context, null, true);
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mLayoutInflater.inflate(R.layout.row_diary, parent, false);
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
final int idCraving = cursor.getInt(cursor.getColumnIndex(ContentProvider.ID));
final ImageView deleteDiaryEntry;
deleteDiaryEntry = (ImageView) view.findViewById(R.id.remove_registry_from_diary);
deleteDiaryEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().getContentResolver().delete(ContentProvider. URI, " _id = ? ", new String[]{"" + idCraving});
getAdapter().notifyDataSetChanged();
}
});
}
}
}
Within the delete() method in your ContentProvider, you need to have the following line of code:
getContext().getContentResolver().notifyChange(uri, null);
From the official documentation, this line will notify CursorAdapter objects that a change occurred. If you do not have this line, you will see the change only when the code does a fresh lookup of the database which occurs when you exit and reopen the app.