Home > database >  Plus/Minus button with a counter but counter does not work properly
Plus/Minus button with a counter but counter does not work properly

Time:06-05

I have 2 buttons and a TextView to update the counter based on how many times the plus or minus button was pressed.

But, the issue is that: (for example) When I press the " " button to 4 and goes down to 3 after pressing "-" button. Then, when I try to press " "(add) button again it jumps up to 5 instead of 4. (i.e. the counter continues adding 1 from when the last time " " button was pressed.

This is the adapter class where the ImageButtons and TextView listeners are implemented

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        //inflate layout flavor_item.xml
        View view = LayoutInflater.from(context).inflate(R.layout.flavor_item, container, false);

        //initialize UID views from flavor_item.xml
        ImageView imageIv = view.findViewById(R.id.imageIv);
        TextView flavorTv = view.findViewById(R.id.flavorTv);
        TextView quantityTv = view.findViewById(R.id.quantityTv);

        ImageButton minusbutton = (ImageButton) view.findViewById(R.id.minusbutton);
        ImageButton plusbutton = (ImageButton) view.findViewById(R.id.plusbutton);

        //getting data
        DashboardFlavorModel model = modelArrayList.get(position);
        String title = model.getTitle();
        int image = model.getImage();
        String qty = model.getQuantity();


        //setting data
        imageIv.setImageResource(image);
        flavorTv.setText(title);
        quantityTv.setText(qty);

        //plusbutton listener
        plusbutton.setOnClickListener(new View.OnClickListener() {
            int count = Integer.parseInt(model.getQuantity());
            @Override
            public void onClick(View view) {
                count  ;
                model.setQuantity("" count);
                quantityTv.setText("" count);
            }
        });

        //listener
        minusbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = Integer.parseInt(model.getQuantity());
                if (i > 0) {
                    i--;
                    model.setQuantity("" i);
                    quantityTv.setText("" i);
                } else{
                    Snackbar.make(view,"Cannot have < 0 QTY",Snackbar.LENGTH_SHORT).setAction("RETRY", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            model.setQuantity("0");
                            quantityTv.setText(model.getQuantity());
                           }[![enter image description here][1]][1]
                    }).show();
                }

            }
        });

(Note***) I tried checking the counter using getter and setter to check whether it worked and it did so I have no idea why when pressing " " after "-" it wouldn't just 1 from the value after "-" button.

CodePudding user response:

try putting

int count = Integer.parseInt(model.getQuantity());

inside onClick for plusbutton onclicklistener

  • Related