解决Listview更新IllegalStateException异常
当我们在使用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
上一篇: 清华大学
推荐阅读
-
mysql5.7更新操作报异常thisisincompatiblewithsql_mode=only_full_group_by的原因和解决办法
-
解决Listview更新IllegalStateException异常
-
IllegalStateException: For MAC signing you do not need to specify the verifier key separately异常解决
-
php更新mysql后获取影响的行数发生异常解决方法
-
php更新mysql后获取影响的行数发生异常解决方法_php技巧
-
php更新mysql后获取影响的行数发生异常解决方法_PHP
-
ListView 的Adapter刷新数据时出现IndexOutOfBoundsException: Index: 4, Size: 0 数组越界异常问题的解决方案
-
php更新mysql后获取影响的行数发生异常解决方法
-
Ajax开发中的经典异常解决[前端](持续更新)
-
php更新mysql后获取影响的行数发生异常解决方法