简体   繁体   中英

Cannot remove item from shopping cart

I want remove item from shopping cart.
I am getting following error:

Process: com.example.myapp, PID: 21655
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.remove(ArrayList.java:503)
at com.example.myapp.CartActivity.onCartItemDelete(CartActivity.java:191)
at com.example.myapp.Adapter.CartAdapter$CartViewHolder.onClick(CartAdapter.java:96)

My Adapter Code

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {

    private List<CoffeeOrder> listData = new ArrayList<>();
    Context context;
    private OnCartListener mOnCartListener;


    public  CartAdapter(Context context, List<CoffeeOrder> listData, OnCartListener onCartListener){
        this.listData = listData;
        this.context = context;
        this.mOnCartListener = onCartListener;
    }


    @NonNull
    @Override
    public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.cart_item , parent, false);
        return new CartViewHolder(v,mOnCartListener);
    }

    @Override
    public void onBindViewHolder(@NonNull CartViewHolder holder, int position) {

        CoffeeOrder order = listData.get(position);
        holder.txt_cart_name.setText( order.getProductName());
        holder.txt_price.setText(order.getPrice() + " \u20ac");


        TextDrawable drawable = TextDrawable.builder()
                .buildRound(""+ listData.get(position).getQuantity(), Color.BLACK);
        holder.img_cart_count.setImageDrawable(drawable);


        Double double_total;
        double_total = 0.00;

        double_total += (Double.parseDouble(order.getPrice()))*(Double.parseDouble(order.getQuantity()));

        //two decimal number
        String total = String.format("%.2f", double_total);

        holder.txt_price.setText(total + " \u20ac");



    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    public static class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView txt_cart_name,txt_price;
        public ImageView img_cart_count;
        public ImageView itemDeleteBtn;
        OnCartListener onCartListener;



        public CartViewHolder(View itemView, OnCartListener onCartListener){
            super(itemView);
            txt_cart_name = itemView.findViewById(R.id.cart_item_name);
            txt_price = itemView.findViewById(R.id.cart_item_price);
            img_cart_count = itemView.findViewById(R.id.cart_item_count);
            itemDeleteBtn = itemView.findViewById(R.id.btn_item_delete);

            this.onCartListener = onCartListener;

            itemDeleteBtn.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            onCartListener.onCartItemDelete(getAdapterPosition());
        }


    }


    public interface OnCartListener{
        void onCartItemDelete(int position);
    }


}

My activity Method Code

@Override
public void onCartItemDelete(int position) {
    listCart.remove(position);
}

Seems like you are removing an item from index 1 but your array is just 1 item long

what you have 
[0]

what you want to delete 
[0][1]
    ^

So try to change

 @Override
        public void onClick(View v) {
            onCartListener.onCartItemDelete(getAdapterPosition());
        }

to

@Override
        public void onClick(View v) {
            onCartListener.onCartItemDelete(getAdapterPosition()-1);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM