[英]java.lang.IndexOutOfBoundsException : When i try to remove item from list of recyclerview
When ever i try to delete last item from list it gives me IndexOutOfBound Error, its happen only with last item from list.当我尝试从列表中删除最后一项时,它会给我 IndexOutOfBound 错误,它只发生在列表中的最后一项。
Here is my code这是我的代码
public class CartAdapter extends RecyclerView.Adapter<CartViewHolder> {
private List<Order> listData = new ArrayList<>();
private CartActivity cartActivity;
private int total;
public CartAdapter(List<Order> listData, CartActivity cartActivity) {
this.listData = listData;
this.cartActivity = cartActivity;
}
@NonNull
@Override
public CartViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(cartActivity);
View itemView = inflater.inflate(R.layout.cart_layout, viewGroup, false);
return new CartViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final CartViewHolder cartViewHolder, @SuppressLint("RecyclerView") final int i) {
cartViewHolder.counterButton.setNumber(listData.get(i).getQuantity());
cartViewHolder.counterButton.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
@Override
public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
total = 0;
Order order = listData.get(i);
order.setQuantity(String.valueOf(newValue));
new Database(cartActivity).updateCart(order);
calculate();
}
});
cartViewHolder.txt_price.setText(listData.get(i).getPrice());
cartViewHolder.txt_cart_name.setText(listData.get(i).getFoodName());
cartViewHolder.removeItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listData.remove(i);
notifyItemRemoved(i);
notifyItemRangeChanged(i, listData.size());
Order order = listData.get(i);
new Database(cartActivity).clearItem(order);
calculate();
}
});
}
private void calculate(){
for (Order orders: listData){
total += Integer.parseInt(orders.getPrice()) * Integer.parseInt(orders.getQuantity());
Log.d("GDGDGD", String.valueOf(total));
}
int cgst = (total * 5)/100;
total += cgst;
Locale locale = new Locale("en","IN");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
cartActivity.txtTotalPrice.setText(fmt.format(total));
}
}
Above code is snippet from recyclerview, item fetched from the database and list made.上面的代码是来自 recyclerview 的片段,从数据库中提取的项目和列表。
The issue is this line: Order order = listData.get(i);
问题是这一行:
Order order = listData.get(i);
You are removing the item first and then you are trying to get the order.您先移除商品,然后再尝试获取订单。
listData.remove(i); // remove item from listData: it will have zero items if earlier, listData had one item
...
Order order = listData.get(i); // get ith order: will crash if listData's size is 0
Placing listData.remove(i);
放置
listData.remove(i);
at the end of the function will solve the issue.在函数结束时将解决问题。
Like this:像这样:
cartViewHolder.removeItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
notifyItemRemoved(i);
notifyItemRangeChanged(i, listData.size());
Order order = listData.get(i);
new Database(cartActivity).clearItem(order);
listData.remove(i);
calculate();
}
});
Edit:编辑:
There is one more issue in calculate().在calculate() 中还有一个问题。 You have to reassign the variable
total
to zero.您必须将变量
total
重新分配为零。
Like this:像这样:
private void calculate(){
total = 0;
for (Order orders: listData){
total += Integer.parseInt(orders.getPrice()) * Integer.parseInt(orders.getQuantity());
Log.d("GDGDGD", String.valueOf(total));
}
int cgst = (total * 5)/100;
total += cgst;
Locale locale = new Locale("en","IN");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
cartActivity.txtTotalPrice.setText(fmt.format(total));
}
And remove total = 0;
并删除
total = 0;
assignment.任务。 Like this:
像这样:
cartViewHolder.counterButton.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
@Override
public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
Order order = listData.get(i);
order.setQuantity(String.valueOf(newValue));
new Database(cartActivity).updateCart(order);
calculate();
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.