notifyDataSetChanged没有更新的ListView [英] notifyDataSetChanged not updating ListView

查看:170
本文介绍了notifyDataSetChanged没有更新的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过所有关于这个问题的职位,但我还是不能让我的ListView更新。
我扩展ListActivity。
这里是我的ArrayAdapter:

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);
 }

现在,当我删除一个文件我想在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);
    }
}

在我BindAllSongs();方法:

In my BindAllSongs(); method:

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

这个伟大的工程(文件正在从SD卡中删除),但从来没有列表的更新。当我导航离开,然后返回到ListView,该项目已经一去不复返了。 (返回到ListView调用BindAllSongs();一次。)
感谢您的帮助,如果你需要更多的信息让我知道。

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.

推荐答案

在一个ArrayAdapter的值实际上不依赖于文件系统。您需要调用remove上被删除的项目(S),然后你可以调用notifyDataSetChanged(虽然你可能不需要)。

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).

这篇关于notifyDataSetChanged没有更新的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆