I'm using a database, and there's a list fragment which use a cursor adapter to I get by querying the databse in a loader.
When the user press a list view item long press, he sees a context menu and a option to delete this entry.
When he press delete I'm starting a thread that deletes this entry and then start the loader again to get a new cursor (becuase "requery()" is deprecated).
When the loader finishes to load the new cursor I'm trying to use changeCursor
method that suppose to refresh the list view but it doesn't do it, so I tried using notifyDataSetChanged
but it doesn't work too.
I've checked and the cursor coming back from the loader and it does change the cursor but the list view doesn't refresh.
What should I do? Restart the fragment?
Here's some code:
case MENU_REMOVE:
final AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
mProgress = ProgressDialog.show(getActivity(), getString(R.string.list_remove_progress_title_text),
getString(R.string.progress_dialog_description));
new Thread(new Runnable() {
@Override
public void run() {
PlacesHandler wph = new PlacesHandler(getActivity());
wph.DeleteEntry(mPlaceName, info.id);
getLoaderManager().initLoader(0, null, ListFragment.this);
}
}).start();
return(true);
When finished loading:
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if(mProgress == null || !mProgress.isShowing()){
...
}
else{
mProgress.dismiss();
mAdapter.changeCursor(data);
mAdapter.notifyDataSetChanged();
}
}
Thanks!
Thanks everyone but I found my problem! The cursor that was returned from the loader was the same cursor as what was in the adapter. This is because I called:
getLoaderManager().initLoader(0, null, this);
instead of:
getLoaderManager().restartLoader(0, null, this);
So because I've already used the loader with the ID 0 when I first loaded the cursor when the fragment was created, it just returned me the cursor immediately.
So if some people face the same problem and read it, just know that initLoader
uses an existing loader with the specified ID if there is one. If there isn't, it creates one.
But if you want to discard the loader's old data and start over you should use restartLoader
.
Again, thanks for anyone who tried to help!