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.
关于Android在listview刷新时候报这个错误,大概是因为加载数据和刷新数据不在同一个线程导致adapter.getCount与list.size 不对应而报的错误
解决办法
尽量让加载数据和刷新数据在同一线程执行
我的解决办法是在自定义adapter不绑定数据源,直接把数据源写在adapter里面,需要刷新数据时候调用这个方法就可以让加载数据和刷新数据在同一线程了.
notifyDataSetChanged(list);
class LvAdapter extends BaseAdapter
{
private ArrayList<HashMap<String, Object>> data = null;
private Context mContext = null;
public LvAdapter(Context context)
{
data = new ArrayList<HashMap<String, Object>>();
this.mContext = context;
}
public void notifyDataSetChanged(ArrayList<HashMap<String, Object>> data)
{
this.data.clear();
this.data.addAll(data);
this.notifyDataSetChanged();
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
return convertView;
}
}
小白第一次发帖,有什么不对的请大神指点下.