java.lang.IndexOutOfBoundsException:当我尝试从recyclerview列表中删除项目时

时间:2018-12-30 06:28:17

标签: java android android-recyclerview indexoutofboundsexception

每当我尝试删除列表中的最后一个项目时,都会出现IndexOutOfBound错误,它仅在列表中的最后一个项目中发生。

这是我的代码

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

以上代码是来自recyclerview的代码段,是从数据库中获取的项目并列出了列表。

1 个答案:

答案 0 :(得分:0)

问题是此行:Order order = listData.get(i);

您要先删除商品,然后再尝试获取订单。

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

在函数末尾放置listData.remove(i);将解决此问题。

赞:

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

编辑:

calculate()中还有一个问题。您必须将变量total重新分配为零。<​​/ p>

赞:

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

并删除total = 0;分配。像这样:

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