天天看点

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receiv

在listview刷新数据的时候出现了以下的错误问题:

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not 
receive a notification. Make sure the content of your adapter is not modified from a background thread, 
but only from the UI thread. [in ListView(2131361851, class android.widget.ListView) with
Adapter(class com.adth.adthtv.adapter.Box_name_adapter)]
           

为什么会出现这个异常呢?因为我在Handler中处理的时候直接就调用了adapter.notifyDataSetChanged(),提示我要在主线程更新。

我是这样处理的:

public class Box_name_adapter extends BaseAdapter {
	private Context context;
	private ArrayList<Map<String, Object>> list = null;
	private LayoutInflater inflater = null;
	public Box_name_adapter(Context context,ArrayList<Map<String, Object>> list){
		this.context = context;
		this.list = list;
		this.inflater = LayoutInflater.from(context);
		selectItem = 0; 
	}
	
	@SuppressWarnings("unchecked")  
    public void setBoxList(ArrayList<Map<String, Object>> list1) {  
        if (list1 != null) {  
            list = (ArrayList<Map<String, Object>>) list1.clone();  
           
            notifyDataSetChanged();  
        }  
    }  
  
    public void clearBoxList() {   
        if (list != null) {  
            list.clear();  
        }  
        notifyDataSetChanged();  
    }  
   ......
}
           

这里写了一个setBoxList(ArrayList<Map<String,Object>> list),里面的list数据发生改变时,就会调用notifyDataSetChanged(),刷新listview显示啦!clearBoxList(),当数据清空时刷新listview显示了。

如果您有其他更好的解决办法的话,欢迎留言!!谢谢!!