android – Update one integer in SQLite from RecyclerView List-ThrowExceptions

Exception or error:

I have one activity named cart (Shopping Cart). I use SQLite to store the items and I display the data in the RecyclerView. My problem is when I need to increase or decrease the quantity because my code takes the default quantity (1) and the increase is equal with 2 all-time and decreases with 0. I need to update my DataBase I know that, but I really don’t understand how. I see 2 alternatives with Sqlexec and ContentValues, but I really don’t know how to implement it. If someone can help me, I will really appreciate it. I put my adapter class here.

public class cartItemRecyclerView extends RecyclerView.Adapter<cartItemRecyclerView.ViewHolder>{

    ArrayList<items> itemsList;
    Context context;

    interface IImageButtonListener{
        void onImageButtonClick(View view,int pos,boolean isDecrease);
    }

    public cartItemRecyclerView(Context context, ArrayList<items> itemsList){
        this.context = context;
        this.itemsList = itemsList;
    }

    public cartItemRecyclerView(ArrayList<items> item) {
        this.itemsList = item;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View contactView = inflater.inflate(R.layout.list_cart_item_view,parent,false);

        // Return a new holder instance
        ViewHolder viewHolder = new ViewHolder(contactView);

        return viewHolder;
    }

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

        // Get the data model based on position
        items itm = itemsList.get(position);

        // Set item views based on your view and data model

        holder.nameC.setText(itm.getlName());
        holder.iconC.setImageResource(itm.getlImageId());
        holder.priceC.setText(itm.getlPrice()+ "");
        holder.quantityC.setText(itm.getQuantity()+"");

        holder.setListener(new IImageButtonListener() {
            @Override
            public void onImageButtonClick(View view, int pos, boolean isDecrease) {
                if(isDecrease){
                    int i = itemsList.get(pos).getQuantity();
                    i++;
                    // Update in database
                    holder.quantityC.setText(i+"");

                }
                else {
                    int i = itemsList.get(pos).getQuantity();
                    i--;
                    // Update in database
                    holder.quantityC.setText(i+"");
                }

            }
        });

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView nameC, priceC, quantityC;
        public ImageView increaseC, decreaseC, iconC;
        IImageButtonListener listener;

        public void setListener(IImageButtonListener listener){
            this.listener = listener;
        }

        public ViewHolder(View itemView){
            super(itemView);

            nameC = itemView.findViewById(R.id.name_item_cart);
            priceC = itemView.findViewById(R.id.price_item_cart);
            quantityC = itemView.findViewById(R.id.textview_quantity_items);
            increaseC = itemView.findViewById(R.id.image_increase_quantity);
            decreaseC = itemView.findViewById(R.id.image_decrease_quantity);
            iconC = itemView.findViewById(R.id.icon_item_cart);

            // Event
            increaseC.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onImageButtonClick(v, getAdapterPosition(),true);
                }
            });

            decreaseC.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onImageButtonClick(v, getAdapterPosition(),false);
                }
            });
        }
    }

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *