Search code examples
androidandroid-radiogroup

radio group, calculating answers twice


My issue must have a simple answer and I'm sure I'm missing something obvious... Could anyone point it out for me?

When I run the app on my phone the radio group behaves properly (meaning the score is 0 for the incorrect answer and 1 for the correct answer) however if I click the incorrect answer, then back to the correct answer and resubmit, it adds one more to the score. So the same question would show two points just for clicking the correct answer twice...

package com.example.android.englishquizl3;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import static com.example.android.englishquizl3.R.id.radioButton2;
import static com.example.android.englishquizl3.R.id.radioButton3;

public class MainActivity extends AppCompatActivity {

int baseScore = 0;
private RadioGroup radioGroup1;
private RadioGroup radioGroup2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize Radio Group and attach click handler
    radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
    radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
    radioGroup1.clearCheck();
    radioGroup2.clearCheck();

    // Attaches checkedChangeListener to radio group
    radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton your = (RadioButton) findViewById(radioButton2);

            if (your.isChecked()) {
                baseScore = baseScore + 1;
            } else {
            }

            int score = 1;
            if (your.isChecked()) score += 1;

            switch (score) {
                case R.id.radioButton2:
                    if (your.isChecked()) ;
                    score++;
                default:
                    break;
            }
        }
    });

    radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton theyre = (RadioButton) findViewById(radioButton3);

            if (theyre.isChecked()) {
                baseScore = baseScore + 1;
            } else {
            }

            int score = 1;
            if (theyre.isChecked()) score += 1;

            switch (score) {
                case R.id.radioButton3:
                    if (theyre.isChecked()) ;
                    score++;
                default:
                    break;
            }
        }
    });

}

public void onSubmit(View v) {
    RadioButton rb1 = (RadioButton) radioGroup1.findViewById(radioGroup1.getCheckedRadioButtonId());
    RadioButton rb2 = (RadioButton) radioGroup2.findViewById(radioGroup2.getCheckedRadioButtonId());
    Toast.makeText(MainActivity.this, "Score: " + baseScore, Toast.LENGTH_SHORT).show();
}
}

And my xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:text="1) I think _______ cat is lovely."
    android:textColor="#000000" />

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="you're" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="your" />
</RadioGroup>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:text="2) _______ going to shop tomorrow."
    android:textColor="#000000" />

<RadioGroup
    android:id="@+id/radioGroup2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="They're" />

    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Their" />
</RadioGroup>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/submitBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onSubmit"
        android:text="Submit" />

</LinearLayout>


Solution

  • you are only adding 1 to the baseScore so each time you change the state and have the correct answer you add one more. You would have to reduce the baseScore if you check the wrong number or just set it to 1