解决Listview更新IllegalStateException异常

2022-08-01,,,,

        当我们在使用listview时,数据在跟新的时候频繁滑动容易造成此异常,java.lang.IllegalStateException:。非法状态异常。报以下错误。

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. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131165255, class android.widget.ListView) with Adapter(class com.example.administrator.myxbhdemo.MyAdapter)

        原因是当istView绑定了的list发生了变化而没来得及通知listView导致了上述的异常,所以我们只要listView与list绑定后,在listView显示之前不要让list发现变化就行了。我的做法是在adapter中接收的list和操作的list是两个:

第一步:定义集合
private List<MyData> listdata;//接收传过来的数据
private List<MyData> list = new ArrayList<>();//本地操作数据
第二步:接收
public MyAdapter(List<MyData> list, Context context) {
    this.listdata = list;//接收数据
    this.context = context;
    list.addAll(listdata);//将数据添加得到本地
    inflater = LayoutInflater.from(context);
}
第三步重写
//重写更新方法
@Override
public void notifyDataSetChanged() {
    this.list.clear();
    this.list.addAll(listdata);

    super.notifyDataSetChanged();
}

 

 

完整adapter:

public class MyAdapter extends BaseAdapter {
    private List<MyData> listdata;//接收传过来的数据
    private List<MyData> list = new ArrayList<>();//本地操作数据
    private Context context;
    private final LayoutInflater inflater;

    public MyAdapter(List<MyData> list, Context context) {
        this.listdata = list;//接收数据
        this.context = context;
        list.addAll(listdata);//将数据添加得到本地
        inflater = LayoutInflater.from(context);
    }

    //重写更新方法
    @Override
    public void notifyDataSetChanged() {
        this.list.clear();
        this.list.addAll(listdata);

        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        } else
            return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.rvlon = convertView.findViewById(R.id.tv_rvlon);
        ...

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.rvlon.setText(list.get(position).getRvLon() + "");
       ...

        return convertView;
    }

    class ViewHolder {
        private TextView rvlon;
       ...

    }
}

转发标明出处:https://blog.csdn.net/qq_35698774/article/details/107366610

android互助群:

感谢:

https://blog.csdn.net/u010678460/article/details/52917497?utm_source=blogxgwz0

https://blog.csdn.net/weixin_33982670/article/details/86048708

本文地址:https://blog.csdn.net/qq_35698774/article/details/107366610

《解决Listview更新IllegalStateException异常.doc》

下载本文的Word格式文档,以方便收藏与打印。