mercredi 5 avril 2017

how to prevent reselected checkbox from adding more value

i have created a survey layout with a question and then you can select below avg, avg or above avg. a score is at the bottom of the page and it updates on click via listener.

I have a problem with the customer changing their answer and it not removing the pervious value.

so below is 1 and avg is 2 and above is 3. if you click above it adds 3 and then you change your mind and want to pick avg and you click it and it adds 2 but never removes then pervious 3.

help

    below32 = (CheckBox) findViewById(R.id.below32);
    below32.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (below32.isChecked()) {
                sum += 1;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
                avg32.setChecked(false);
                above32.setChecked(false);
            }
            else {
                sum -= 1;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
            }
        }
    });
    avg32 = (CheckBox) findViewById(R.id.avg32);
    avg32.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (avg32.isChecked()) {
                sum += 2;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
                below32.setChecked(false);
                above32.setChecked(false);
            }
            else {
                sum -= 2;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
            }
        }
    });
    above32 = (CheckBox) findViewById(R.id.above32);
    above32.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (above32.isChecked()) {
                sum += 3;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
                below32.setChecked(false);
                avg32.setChecked(false);
            }
            else {
                sum -= 3;      double total = 100*sum/69;
                result.setText(String.format("%.1f", total) + "%");
            }
        }
    });

what do i need to change or do to make it work? all other attempts have failed.




Aucun commentaire:

Enregistrer un commentaire