masmic
masmic

Reputation: 3564

ListFragment doesn't refresh

INTRODUCTION

I've got an app which consists on an Acitivity where I create some elements and then I save this elements on a ListFragment. This stuff works fine, but the problem comes when I try to delete an element from the list.

Briefly, the listFragment doesn't refresh when I delete an element. If I go back to main Activity and then I enter again to the ListFragment, then the element that I deleted doesn't appear, but the thing would be to refresh this list at the moment I delete an element.

Have to say that I'm a bit confused because at first, it was doing this right, but I don't know what I have touched that now does not do it.

CODE

This are relevant code snippets of ListFragment:

public class MyPlacesListFragment extends ListFragment {
//...

    final class PlacesCursorAdapter extends ResourceCursorAdapter {
    //...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getAdapter();
    }

    public void getAdapter() {

        Cursor c = getActivity().getContentResolver().query(PlacesProvider.CONTENT_URI, PROJECTION, null, null, null);

        mCursorAdapter = new PlacesCursorAdapter(getActivity(), c);
        setListAdapter(mCursorAdapter);
    }

    private void deleteItem(long id){

            Uri uri = ContentUris.withAppendedId(PlacesProvider.CONTENT_URI, id);
            getActivity().getContentResolver().delete(uri, null, null);
    }

I have to say that I work with dataBase and ContentProvider, but these work fine, I've tested them with other apps.

Also, I call notifyChange() on the Insert, Update, and Delete methods of the Provider this way:

getContext().getContentResolver().notifyChange(uri, null);

Upvotes: 0

Views: 127

Answers (1)

FD_
FD_

Reputation: 12919

Just call this:

mCursorAdapter.notifyDataSetChanged();

After you made changes to your underlying data.

Also, I'd suggest renaming your getAdapter() method, as it has a misleading name IMHO. I'd expect it to return always the same adapter, while you seem to use it to initialize a new adapter.

Upvotes: 2

Related Questions