Skip to content Skip to sidebar Skip to footer

Android Refresh Listview After Delete

I'm trying to refresh my listview after the item delete. But it doesnt seems to work. Am i missing something. I used listAdapter.notifyDataSetChanged(); not working either. Could s

Solution 1:

listAdapter.notifyDataSetChanged();

Isn't doing anything because you have not done anything with your adapter. You have not removed anything from the listAdapter, so there is nothing to change. Change onContextMenuItemSelected() so it is like this (so that it works purely with the list positions. MenuItem ids are not the positions in the list):

@OverridepublicbooleanonContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfoinfo= (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    intposition= info.position;
    deletefunc(position);
    returntrue;
    }

Then consider doing listAdapter.remove(listAdapter.getItem(id)) somewhere in deletefunc(). That should automatically call notifyDataSetChanged(); anyways.

Solution 2:

You don't seem to be actually removing the element from the adapters list. You can call listAdapter.remove(myFile) and it will remove the element, as well as call notifyDataSetChanged() for you.

Solution 3:

There really is no reason not to be using Loaders here. You also should not be accessing the File system on the UI thread. A better implementation would be to use an AsyncTaskLoader and move your File access to loadInBackground(). You could also clean this up a bit by extending ListActivity instead of Activity.

Post a Comment for "Android Refresh Listview After Delete"