midiwriter
midiwriter

Reputation: 426

notifyDataSetChanged not updating ListView

I've read all the posts about this issue but I still can't get my ListView to update. I am extending ListActivity. Here's my ArrayAdapter:

 public List<String> songs = new ArrayList<String>();    
 public ArrayAdapter<String> allsongs;

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

    allsongs = new ArrayAdapter<String>(this,
            R.layout.song_items, songs);

 BindAllSongs();        
 ListView listView=getListView();
    registerForContextMenu(listView);
 }

now, when I delete a file I want the ListView to update:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
        case R.id.delete_file:
            File file = new File(Environment.getExternalStorageDirectory().getPath() + "/AudioStreamRecorder/" + getListView().getAdapter().getItem(info.position).toString());
            file.delete();

            runOnUiThread(new Runnable() {
                public void run() {
                    allsongs.notifyDataSetChanged();    
                }
            });

            return true;
        case R.id.cancel:

            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

In my BindAllSongs(); method:

public void BindAllSongs() {
 ...
 ...
setListAdapter(allsongs);
    }

This works great (the files are being deleted from sd card) but the list never updates. When I navigate away, then return to the ListView, the item is gone. (Returning to the ListView calls BindAllSongs(); again.) Thank you for any help, if you need more information let me know.

Upvotes: 0

Views: 2289

Answers (1)

toadzky
toadzky

Reputation: 3846

The values in the ArrayAdapter are not actually tied to the file system. You need to call remove on the item(s) that gets deleted and THEN you can call notifyDataSetChanged (although you may not need to).

Upvotes: 3

Related Questions