简体   繁体   English

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

[英]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.

相关问题 java.lang.IndexOutOfBoundsException:尝试从 RecyclerView 中删除 URL 损坏的项目 - java.lang.IndexOutOfBoundsException : Trying to remove an item with broken URL from RecyclerView 在 RecyclerView 中显示多个数据时出现 java.lang.IndexOutOfBoundsException - java.lang.IndexOutOfBoundsException When Displaying Multiple Data in RecyclerView 尝试在服务器上上传文件时出现java.lang.IndexOutOfBoundsException - java.lang.IndexOutOfBoundsException when try to upload file on server java.lang.IndexOutOfBoundsException:使用try,catch - java.lang.IndexOutOfBoundsException: using try, catch 如何删除java.lang.IndexOutOfBoundsException? - How to remove java.lang.IndexOutOfBoundsException? java.lang.IndexOutOfBoundsException - java.lang.IndexOutOfBoundsException java.lang.IndexOutOfBoundsException - java.lang.IndexOutOfBoundsException 滚动滚动获取RecyclerView异常:-“ java.lang.IndexOutOfBoundsException:检测到不一致。 无效的商品位置21” - On scroll the RecyclerView getting exception : -“ java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 21” java.lang.IndexOutOfBoundsException:无效的索引1,在Sectioned RecyclerView中大小为1 - java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 in Sectioned RecyclerView recyclerview onclicklistener java.lang.IndexOutOfBoundsException:无效的索引0,大小为0 - recyclerview onclicklistener java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM