samedi 30 avril 2016

i want to decrease count when already checked checkbox is unchecked. how do i do this?

i was trying to make a list that will select only 3 symptoms. for which i put a while loop that stops when count becomes 3. count increases when a checkbox is checked but it doesn't decrease when it is unchecked again. How do i solve this. here is what i have done so far:-

JAVA CODE

public void onCheckboxClick (View view) {
        int count = 0;
        String a, b, c;
        CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
        CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
        CheckBox checkBox5 = (CheckBox) findViewById(R.id.checkBox5);
        while(count<3){
            switch (view.getId()) {
                case R.id.checkBox1:
                     if (checkBox1.isChecked()) {
                        if (count == 0) {
                           a = "Bad taste in mouth";
                        } else if (count == 1) {
                        b = "Bad taste in mouth";
                        } else if (count == 2) {
                        c = "Bad taste in mouth";
                     }
                     count++;
                     } else {

                     }
                     break;
                case R.id.checkBox2:
                     if (checkBox2.isChecked()) {
                        if (count == 0) {
                           a = "Gap in between teeth";
                        } else if (count == 1) {
                           b = "Gap in between teeth";
                        } else if (count == 2) {
                           c = "Gap in between teeth";
                     }  
                     count++;
                     } else {

                     }
                     break;
                case R.id.checkBox3:
                     if (checkBox3.isChecked()) {
                        if (count == 0) {
                        a = "Bad breath";
                     } else if (count == 1) {
                        b = "Bad breath";
                     } else if (count == 2) {
                        c = "Bad breath";
                     }
                     count++;
                     } else {

                     }
                     break;
                case R.id.checkBox4:
                     if (checkBox4.isChecked()) {
                        if (count == 0) {
                           a = "Nasal pain";
                        } else if (count == 1) {
                           b = "Nasal pain";
                        } else if (count == 2) {
                           c = "Nasal pain";
                        }
                     count++;
                     } else {

                     }
                     break;
                case R.id.checkBox5:
                     if (checkBox5.isChecked()) {
                        if (count == 0) {
                           a = "Blurred vision";
                        } else if (count == 1) {
                           b = "Blurred vision";
                        } else if (count == 2) {
                           c = "Blurred vision";
                        }
                        count++;

                     } else {

                     }
                     break;
            }
        }
}

XML CODE

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bad taste in mouth"
        android:id="@+id/checkBox1"
        android:onClick="onCheckboxClick"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gap in between teeth"
        android:id="@+id/checkBox2"
        android:onClick="onCheckboxClick"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bad breath"
        android:id="@+id/checkBox3"
        android:onClick="onCheckboxClick"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nasal pain"
        android:id="@+id/checkBox4"
        android:onClick="onCheckboxClick"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Blurred vision"
        android:id="@+id/checkBox5"
        android:onClick="onCheckboxClick"/>

Any help will be appreciated thank you :)




Aucun commentaire:

Enregistrer un commentaire