0

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);
Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
masmic
  • 3,526
  • 11
  • 52
  • 105
  • Did you try to call `notifyDataSetChanged()` on the adapter? – super-qua Feb 17 '14 at 09:51
  • @super-qua I tried but I don't know if I did it OK. Where should I have to call it exactly? – masmic Feb 17 '14 at 09:52
  • @Archer no idea, automatic suggestion really. Not sure about that, I though it had something to do with this – masmic Feb 17 '14 at 09:53
  • chech [this](https://stackoverflow.com/questions/3669325/notifydatasetchanged-example) link for notifyDataSetChanged Example. Which contains the answer like where to use, when to use. & how to use. hope this helps. – i.n.e.f Feb 17 '14 at 10:02
  • Thanks for the link, as I readed there, the notifydataSetChange needs to implement add, insert, etc. methods of the adapter, and here I'm using provider's methods, so this doesn't work for me. Then, what would be the right way to achieve this? – masmic Feb 17 '14 at 10:41

1 Answers1

2

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.

FD_
  • 12,947
  • 4
  • 35
  • 62
  • Where do I have to call that? I tried calling that and didn't work, but maybe i didn't do it on the right place – masmic Feb 17 '14 at 09:54
  • After getContext().getContentResolver().notifyChange – FD_ Feb 17 '14 at 09:56
  • But, I do getContext().getContentResolver().notifyChange on the Provider class extending the ContentProvider. This class is different to the ListFragment, and the CursorAdapter is defined on the ListFragment – masmic Feb 17 '14 at 09:58
  • Ok, sorry. Just do it after you have changed your data. – FD_ Feb 17 '14 at 09:59
  • For example, inside or after the DeleteItem() method? – masmic Feb 17 '14 at 10:01
  • mm, doesn't work doing this. As readed: `For an ArrayAdapter, notifyDataSetChanged only works if you use the add, insert, remove, and clear functions on the Adapter` and I'm not using these adapter methods, I'm using provider's methods to do these things, so... I think maybe the thing is to call Adapter's newView or bindView... but how to do this? – masmic Feb 17 '14 at 10:40