mardi 20 octobre 2020

Sum value with checkboxes in android studio with java?

In my application i use chceck boxes to calculate sum value. If "checkbox1.isChecked" i add value to sum of all data values. When (!checkBox1.isChecked) i dont want to add. 1.)If i do checkBox1 and chceckBox2 my Sum = 300,but if i uncheck checkbox1 it shows me 0.0.(screenshot 1 and 2 )

enter image description hereenter image description here

2.)if i first had checked checkBox1 than i add checkedBox2 it shows me value of checkBox2.(screenshot 3 and 4 )

enter image description here enter image description here

Here is my code? What should a change to make it work properly (add if checked, not add if not checked)


float averageOfMondays = 200;
float averageOfTuesdays = 100;
float daysSum = 0;

  checkBox1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkBox1.isChecked()){
                    monday = true;
                    daysSum+=averageOfMondays;
                    averageCounter++;
                }else  {
                    monday = false;

                    if(averageCounter<=0){ averageCounter = 0; }else{averageCounter--;}
                }
            }
        });

        checkBox2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkBox2.isChecked()){
                    tuesday = true;
                    daysSum+=averageOfTuesdays;
                    averageCounter++;
                }else {
                    tuesday = false;
                    if(averageCounter<=0){ averageCounter = 0; }else{averageCounter--;}
                }
            }
        });
 
btnCheck.setOnClickListener(new View.OnClickListener() {            //button CHECK 
            @Override
            public void onClick(View v) {

                if(averageCounter == 0){
                    tvAverage.setText("Please choose at least 1 day");

                } else{
                    tvAverage.setText(String.valueOf(daysSum));
                    daysSum=0;
                }
            }
        });



Aucun commentaire:

Enregistrer un commentaire