android listview移除一条数据后造成数组越界异常

在适配器里移除一条数据之后就数组越界异常,代码是这样写的
holder.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = data.get(position).getCount();
                count--;
                if (count <= 0) {
                    count = 0;
                    data.remove(data.get(position));
                    
                    return;
                }
                holder.count.setText(count + "");
                data.get(position).setCount(count);
                holder.price.setText((data.get(position).getCommodity_price()) * (data.get(position).getCount()) + "");
                ShopFragment.handler.sendEmptyMessage(1);
            }
        });

解决办法其实很简单

    移除数据后立马刷新下适配器就好啦

holder.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = data.get(position).getCount();
                count--;
                if (count <= 0) {
                    count = 0;
                    data.remove(data.get(position));
                    notifyDataSetChanged();
                    return;
                }
                holder.count.setText(count + "");
                data.get(position).setCount(count);
                holder.price.setText((data.get(position).getCommodity_price()) * (data.get(position).getCount()) + "");
                ShopFragment.handler.sendEmptyMessage(1);
            }
        });

你可能感兴趣的:(问题记录)