dimanche 4 février 2018

if - else if not working

I don't get why this last if-else if is not working.

I am trying to create event when 2 checkBoxes are checked at the same time for a project (quiz application).

I have put a log.v in the listeners to see if the listenCheck is working - and it does. It increments when i check the first two checkboxes(q7R1, q7R2) by one and decrements by two if i check the other two (q7R3,q7R4)

Thank you

    public class Main2Activity extends AppCompatActivity {
int listenCheck=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
    final Button button = findViewById(R.id.sub);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

        }});

    q7R1 = findViewById(R.id.q7checkBox1);
    q7R2 = findViewById(R.id.q7checkBox2);
    q7R3 = findViewById(R.id.q7checkBox3);
    q7R4 = findViewById(R.id.q7checkBox4);


    q7Img = findViewById(R.id.imgQ7);
            q7R1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton1, boolean b) {
            if (q7R1.isChecked()){listenCheck+=1;
                Log.v("Main2Activity","listenchk" + listenCheck);
                }

        }
    });
    q7R2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton2, boolean b) {

            if (q7R2.isChecked()){listenCheck+=1;
                Log.v("Main2Activity","listenchk" + listenCheck);
                }
        }
    });
    q7R3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton3, boolean b) {
            if (q7R3.isChecked()){listenCheck-=2;
                Log.v("Main2Activity","listenchk" + listenCheck);}
        }
    });
    q7R4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton4, boolean b) {
            if (q7R4.isChecked()){listenCheck-=2;
                Log.v("Main2Activity","listenchk" + listenCheck);}
        }
    });
    correctState = (ContextCompat.getColorStateList(getApplicationContext(),R.color.radionbuttonstate));
    wrongState =  (ContextCompat.getColorStateList(getApplicationContext(),R.color.radiobuttonwrong));
    checkAnswers();
}public void q7Check2(){

        if (listenCheck==2){
            score = score + (100/8);
            q7R1.setTextColor(correctState);
            q7R2.setTextColor(correctState);
            q7Img.setImageResource(R.drawable.ddvspan);}
           else if (listenCheck<0){
            q7R3.setTextColor(wrongState);
            q7R4.setTextColor(wrongState);
            Toast.makeText(getApplicationContext(), getResources().getString(R.string.correct7), Toast.LENGTH_SHORT).show();}

        Log.v("Main2Activity","listenchk" + listenCheck);
            }public void checkAnswers(){
    q1Check();
    q2Check();
    q3Check();
    q4Check();
    q5Check();
    q6Check();
    q7Check2();
    q8Check();}

Aucun commentaire:

Enregistrer un commentaire