【问题标题】:notifyDatasetChanged() not update listviewnotifyDatasetChanged() 不更新列表视图
【发布时间】:2014-04-16 06:37:37
【问题描述】:

我在操作栏上有一个微调器,在片段中有一个列表视图。 Spinner includes some folder structures, and when select some folder, it will update spinner data source and clear listview data by notifyDatasetChanged() in onItemSelected call back, then, it will call a runnable to load new data of folder and send a message to handler通过 notifyDatasetChanged() 更新列表视图适配器。问题是当处理程序接收到消息并更新适配器时,列表视图没有改变并且将保持为空,getview() 方法没有被调用。

问题似乎出现在 4.1.1 上,但不是在 4.3 上。

请给我一些建议或解决方法来解决它,因为我需要支持 2.3 或更高版本。

例如

onItemSelected(){
    backward(folder);
}

backward(folder){
    notifyDatasetChanged() for spinner;

    clearListViewDataAndNotifyDatasetChanged();

    loadDataForFolderInRunnable();
}

loadDataForFolderInRunnable(){
    getDataFromHttp();

    sendMessageToHandler();
}

handler(){
    updateDataSourceForListView();

    notifyDatasetChanged();
}

我发现如果我在onItemSelected()回调中调用notifyDatasetChanged(),那么listview就不能再刷新了。

android 的一个项目成员告诉我这个问题已经解决了。有谁知道如何解决它或一些解决方法?

提前致谢!

【问题讨论】:

标签: android listview


【解决方案1】:

可能线程尚未完成加载数据,并且在数据仍在加载时更新了列表视图。对于您的处理程序,请尝试以下操作:

handler.post(new Runnable() {
@Override
public void run() {
    notifyDataSetChanged();
    listView.setAdapter(adapter);
}
});

【讨论】:

  • 感谢您的回复,但它不适用于我。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多