0

I am making a quiz app which there are some question:
enter image description here


When the user clicks NEXT QUESTION, another question will be shown. I have made the current code:

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.number_1:
            if (checked)
                point = 0;
            Toast.makeText(this, ""+ point,
                    Toast.LENGTH_LONG).show();

            break;
        case R.id.number_2:
            if (checked)
                point = 1;
            Toast.makeText(this, ""+ point,
                    Toast.LENGTH_LONG).show();

            break;
        case R.id.number_3:
            if (checked)
                point = 2;
            Toast.makeText(this, ""+ point,
                    Toast.LENGTH_LONG).show();

            break;
        case R.id.number_4:
            if (checked)
                point = 3;
            Toast.makeText(this, ""+ point,
                    Toast.LENGTH_LONG).show();

            break;
    }
}

How can I make this scenario happen when the NEXT QUESTION clicked, another question pop up in the same activity?

Sina Amiri
  • 85
  • 1
  • 9

3 Answers3

0

You have your questionTextView, just use questionTextView.setText(newQuestion)

Set all your radioButton to false/unselected idk the correct thing.


But its probably better to put a FrameLayout in your activity and call a new QuestionFragment every time your user did answer a question and build a new View , your controller will be more independent.

Nek
  • 73
  • 8
0

you can do something like that

public void nextQuestion(View view){

   questionTextView.setText(gtString(R.string.nextQuestion));
   checkBox1.setText("option1");
   checkBox2.setText("option2");
   checkBox3.setText("option3");
   checkBox4.setText("option4");
}

Replace all above view names with yours

Ali Ahsan
  • 460
  • 3
  • 13
0

1. If you are using RadioGroup, then when the NextQestion button get clicked first clear the state of RadioButton calling method RadioGroup.clearCheck().

2. Get the next question from your database or from other place and set values to your TextView and RadioButtons.

Here is an example:

public class Main2Activity extends AppCompatActivity {

    TextView textQuestion;
    RadioGroup radioGroup;
    RadioButton radioButton1,radioButton2,radioButton3;
    Button buttonNextQuestion;

    int questionNo = 1;

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

        textQuestion = (TextView) findViewById(R.id.text_question); 
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
        radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
        buttonNextQuestion = (Button) findViewById(R.id.button_next_question);

        buttonNextQuestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                questionNo++;
                loadNextQuestion();
            }
        });
    }

    public void loadNextQuestion() {

        String question, option1, option2, option3, option4;

        // Get question and options from database or from other place
        // and store into >> question, option1, option2, option3, option4

        // Update UI
        // Clear radio button state
        radioGroup.clearCheck();

        // Set new question and its options to view
        textQuestion.setText(question);
        radioButton1.setText(option1);
        radioButton2.setText(option2);
        radioButton3.setText(option3);
        radioButton4.setText(option4);
    }
}
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61