在适配器里移除一条数据之后就数组越界异常,代码是这样写的

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);

}

});

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐