Rafly Andrian
Rafly Andrian

Reputation: 71

Adding variable automatically when checkbox is checked Android Studio

Is it possible to make arithmetic calculation automatically when checkbox is pressed? I want to add the total harga (total price) change based the choice of checkbox topping prices and add them with the basic prices (product price).

ex: checked CB Extra Ayam and CB Kepala will add 3000 + 4000 then add it with basic product price 10000 so the total price would be 17000 and the total price will back to 10000 when im unchecked CB Extra Ayam and CB Kepala and so on

screenshoot

Here is my code

if (Code.toUpperCase().matches((inMieAyam.toUpperCase()))){
            
    //set text topping
    CBTopping1.setText("Extra Ayam");
    CBTopping2.setText("Extra Tetelan Sapi");
    CBTopping3.setText("Cakar (2 pcs)");
    CBTopping4.setText("Kepala");
    CBTopping5.setText("Bakso Sapi");
    CBTopping6.setText("Telur Mata Sapi");
    CBTopping7.setText("Balungan Rica Ayam");
    CBTopping8.setText("Extra Sawi");
    CBTopping9.setText("Extra Acar");

    //hide remaining topping CB
    CBTopping10.setVisibility(View.GONE);

    //set price to variable
    Price1 = 3000;
    Price2 = 7000;
    Price3 = 4000;
    Price4 = 4000;
    Price5 = 3000;
    Price6 = 4000;
    Price7 = 4000;
    Price8 = 1000;
    Price9 = 1000;
    Price10 = 0;

    //set price topping
    TxtvToppingPrice1.setText("Rp."+Price1);
    TxtvToppingPrice2.setText("Rp."+Price2);
    TxtvToppingPrice3.setText("Rp."+Price3);
    TxtvToppingPrice4.setText("Rp."+Price4);
    TxtvToppingPrice5.setText("Rp."+Price5);
    TxtvToppingPrice6.setText("Rp."+Price6);
    TxtvToppingPrice7.setText("Rp."+Price7);
    TxtvToppingPrice8.setText("Rp."+Price8);
    TxtvToppingPrice9.setText("Rp."+Price9);


    //hide remaining topping price Txtv
    TxtvToppingPrice10.setVisibility(View.GONE);
    

    FinalPrice1 = 0;
    FinalPrice2 = 0;
    FinalPrice3 = 0;
    FinalPrice4 = 0;
    FinalPrice5 = 0;
    FinalPrice6 = 0;
    FinalPrice7 = 0;
    FinalPrice8 = 0;
    FinalPrice9 = 0;
    FinalPrice10 = 0;

    CBTopping1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice1 = Price1;
            }
            else {
                FinalPrice1 = 0;
            }
        }
    });

    CBTopping2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice2 = Price2;
            }
            else {
                FinalPrice2 = 0;
            }
        }
    });

    CBTopping3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice3 = Price3;
            }
            else {
                FinalPrice3 = 0;
            }
        }
    });

    CBTopping4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice4 = Price4;
            }
            else {
                FinalPrice4 = 0;
            }
        }
    });

    CBTopping5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice5 = Price5;
            }
            else {
                FinalPrice5 = 0;
            }
        }
    });

    CBTopping6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice6 = Price6;
            }
            else {
                FinalPrice6 = 0;
            }
        }
    });

    CBTopping7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice7 = Price7;
            }
            else {
                FinalPrice7 = 0;
            }
        }
    });

    CBTopping8.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice8 = Price8;
            }
            else {
                FinalPrice8 = 0;
            }
        }
    });

    CBTopping9.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                FinalPrice9 = Price9;
            }
            else {
                FinalPrice9 = 0;
            }
        }
    });


    FoodPrice1 = Integer.parseInt(Price);
    TotalFinalPrice = FoodPrice1 + FinalPrice1 + FinalPrice2 + FinalPrice3 +
            FinalPrice4 + FinalPrice5 + FinalPrice6 +
            FinalPrice7 + FinalPrice8 + FinalPrice9 ;

    TxtvTotalHarga.setText(Integer.toString(TotalFinalPrice));

}

edit : turn out the variables is not changed in the checkbox listener

default FinalPrice1 is 0 when im changed it from checkbox listener it would not change to Price1 values, it still 0. I tried to change it directly by writing value to it FinalPrice1 = 10000 also no luck it just say 0.

if I remove the

            FinalPrice1 = 0;
            FinalPrice2 = 0;
            FinalPrice3 = 0;
            FinalPrice4 = 0;
            FinalPrice5 = 0;
            FinalPrice6 = 0;
            FinalPrice7 = 0;
            FinalPrice8 = 0;
            FinalPrice9 = 0;
            FinalPrice10 = 0;

it would be nullpointer error i dont know whats wrong

i cant paste the whole code because the post didn't allow it, its too long lmao

the variable of the TotalFinalPrice just not changed or maybe not calculated

Upvotes: 0

Views: 241

Answers (2)

Tiny Sonhh
Tiny Sonhh

Reputation: 141

I think you should first handle your objects into arrays, using tag value to save initial prices, create one event listener ( handle using object'd getTag() or array index....

And important: move the setOn..... out of the if. These call should be done onStart, before you trigger this event.

And can yoi also give more code on the context that this big if is calling?

Did you debug with AS to see these events triggered?

Upvotes: 1

RitchyCZE
RitchyCZE

Reputation: 75

When your TextView have to be updated, use invalidate() method. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().

For your case use TxtvTotalHarga.invalidate();.

Upvotes: 1

Related Questions