在通知listview有时候,偶尔会冒出这样的错误:
Make sure the content of your adapter is not modified from a background thread, but only from the UI thread
就是把获得数据源的代码同时放到了线程里面,及时通知更新,这样就没有问题,欢迎拍砖!!!
The content of the adapter has changed but ListView did not receive a notification.
看了一下网上很多的分析,原因就是listview绑定的数据源改变了,没有及时的通知适配器去更新界面,我遇到的代码如下:
// 删除成功之后,刷新界面
protected void refreshUI() {
maps.remove(position);
initFirstData();
fireTableDataChange();
}
// 初始化加载控件
private void initProgress() {
progressDialog = DialogManager.showProgressDialog(this.getActivity(),
"", "數據加載中...", DialogType.CloseHis);
}
// 首次加載頁面,初始化数据,更新界面
private void inittipspriceData() {
progressDialog.show();
new Thread() {
@Override
public void run() {
try {
refreshUI();
} finally {
Runnable work = new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
}
};
handler.post(work);
}
}
}.start();
}
// 通知适配器更新
public void fireTableDataChange</span>() {
if (tipspriceAd == null) {
return;
}
handler.post(new Runnable() {
@Override
public void run() {
tipspriceAd.notifyDataSetChanged();
}
});
}
我新起了线程,对界面和数据进行更新,但有时候就会遇到上述的错误,后来我修改如下代码:
// 初始化加载控件
private void initProgress() {
progressDialog = DialogManager.showProgressDialog(this.getActivity(),
"", "數據加載中...", DialogType.CloseHis);
}
// 首次加載頁面,初始化数据,更新界面
private void inittipspriceData() {
progressDialog.show();
new Thread() {
@Override
public void run() {
try {
refreshUI();
} finally {
Runnable work = new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
}
};
handler.post(work);
}
}
}.start();
}
// 通知适配器更新
public void refreshUI() {
if (tipspriceAd == null) {
return;
}
handler.post(new Runnable() {
@Override
public void run() {
initFirstData();
tipspriceAd.notifyDataSetChanged();
}
});
}
就是把获得数据源的代码同时放到了线程里面,及时通知更新,这样就没有问题,欢迎拍砖!!!