0

I'm new to android, so basically i made a really simple Exam, consisting of 3 questions, each has 2 answers. But everytime I check one, then "realize" I made a mistake, and want to change my choice, the first choice does not disappear. So I'm left with 2 checked answers.

Below is my main activity Java code:

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    RadioGroup rg1, rg2, rg3;
    ImageView img;
    Button submit;
    boolean q1=false, q2=false, q3=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rg1 = (RadioGroup)findViewById(R.id.rg1);
        rg2 = (RadioGroup)findViewById(R.id.rg2);
        rg3 = (RadioGroup)findViewById(R.id.rg3);
        rg1.setOnCheckedChangeListener(this);
        rg2.setOnCheckedChangeListener(this);
        rg3.setOnCheckedChangeListener(this);
        img = (ImageView)findViewById(R.id.img_id);
        submit = (Button)findViewById(R.id.submit);
        submit.setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup rg, int id) {
        if(rg == rg1){
            q1= id == R.id.q1a1;
        }
        else if(rg==rg2) {
            q2 = id == R.id.q2a1;
        }
        else if(rg==rg3) {
            q3 = id == R.id.q3a1;
        }
    }
    @Override
    public void onClick(View v) {
        if(v==submit)
        {
            if(q1&&q2&&q3)
                img.setImageResource(R.drawable.img_1);
            else
                img.setImageResource(R.drawable.img_2);

        }

    }
}
natitati
  • 35
  • 4

1 Answers1

0

There is a simple solution to this

Have an int array with choices represented as

[0, 0, 0] -> render your layout with this choice at the beginning then

then once some value is checked [1, 0, 0] -> rerender

[1, 2, 0] -> rerender now if you select the third option make the

1 value as 0
2 as 1 
and 0 as 2]

such that the array becomes [0,1,2] 

and then rerender the whole radiobutton section.

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31