0
votes

I have a multiple choice test that is created dynamically.
Each time you press a button it shows a different, random question. Also, I placed the right answer as well as three wrong answers into an array and put them into radio buttons.

This works so far as in each time you click the button it shows a new, random question with all the answers scrambled.

What I am trying to do now is check the answer selected against the right answer I put into a radio button. I can successfully retrieve the ID of the radiobutton selected.

Now I need to check that radio button against the right answer which is stored in the String variable "y". How would I do this?

I though I could return the text of the radiobutton into a string and then match it against String y. I am having trouble getting the text of that radiobutton into a string though. Is that possible or is there a different way I can do this? My code is as follows:

public void nextQuestion(View view){
    TextView questionsTextView = (TextView)findViewById(R.id.questions);
    RadioButton answerOne = (RadioButton)findViewById(R.id.answerOne);
    RadioButton answerTwo = (RadioButton)findViewById(R.id.answerTwo);
    RadioButton answerThree = (RadioButton)findViewById(R.id.answerThree);
    RadioButton answerFour = (RadioButton)findViewById(R.id.answerFour);

    Button next = (Button)findViewById(R.id.next);

    int i = questions.size();
    if(i == 0){
        questionsTextView.setText("You have finished the Test!");
        next.setText("Submit");
    }
    else {
        //Show Radio Buttons
        answerOne.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        answerTwo.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        answerThree.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        answerFour.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        //Pull Random Question
        Random randomGenerator = new Random();
        String randQuestion = questions.get(randomGenerator.nextInt(questions.size()));
        //Get index number of randomly pulled question
        int x = questions.indexOf(randQuestion);
        //Remove randomly selected question from array
        questions.remove(randQuestion);
        //Show randomly selected question
        questionsTextView.setText(randQuestion);
        //Place correct answer into string "y"
        String y = answers.get(x);
        //Create array to hold answers
        List<String> answersForQuestion = new ArrayList<>();
        //Add right answer to answersForQuestion array
        answersForQuestion.add(y);
        //Add wrong answers to answersForQuestions array
        String randWrongAnswers1 = wrongAnswers.get(randomGenerator.nextInt(wrongAnswers.size()));
        answersForQuestion.add(randWrongAnswers1);
        wrongAnswers.remove(randWrongAnswers1);
        String randWrongAnswers2 = wrongAnswers.get(randomGenerator.nextInt(wrongAnswers.size()));
        answersForQuestion.add(randWrongAnswers2);
        wrongAnswers.remove(randWrongAnswers2);
        String randWrongAnswers3 = wrongAnswers.get(randomGenerator.nextInt(wrongAnswers.size()));
        answersForQuestion.add(randWrongAnswers3);
        wrongAnswers.remove(randWrongAnswers3);
        //Randomize all answers
        String randomizeOrderOfAnswers1 = answersForQuestion.get(randomGenerator.nextInt(answersForQuestion.size()));
        answerOne.setText(randomizeOrderOfAnswers1);
        answersForQuestion.remove(randomizeOrderOfAnswers1);
        String randomizeOrderOfAnswers2 = answersForQuestion.get(randomGenerator.nextInt(answersForQuestion.size()));
        answerTwo.setText(randomizeOrderOfAnswers2);
        answersForQuestion.remove(randomizeOrderOfAnswers2);
        String randomizeOrderOfAnswers3 = answersForQuestion.get(randomGenerator.nextInt(answersForQuestion.size()));
        answerThree.setText(randomizeOrderOfAnswers3);
        answersForQuestion.remove(randomizeOrderOfAnswers3);
        String randomizeOrderOfAnswers4 = answersForQuestion.get(randomGenerator.nextInt(answersForQuestion.size()));
        answerFour.setText(randomizeOrderOfAnswers4);
        answersForQuestion.remove(randomizeOrderOfAnswers4);
        //Remove answer from array
        answers.remove(y);
        //Add users answer to array
        RadioGroup g = (RadioGroup)findViewById(R.id.radioGroup);
        int selected = g.getCheckedRadioButtonId();
        String selectedString = Integer.toString(selected);

        questionsTextView.setText(selectedString);

        /*// Gets a reference to our radio group
        // rBtnDigits is the name of our radio group (code not shown)
        RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits);

        // Returns an integer which represents the selected radio button's ID
        int selected = g.getCheckedRadioButtonId();

        // Gets a reference to our "selected" radio button
        RadioButton b = (RadioButton) findViewById(selected);

        // Now you can get the text or whatever you want from the "selected" radio button
        b.getText();*/



        next.setText("Next Question");
    }
1
radiobutton.getText().toString()Itzik Samara

1 Answers

0
votes

First of all i think you have a big trouble with a clear logic in your game/app.
Why you don't want use an OOP?

However let's talking about your question.

I am having trouble getting the text of that radiobutton into a string though.

Why you having trouble? What exactly you can't doing?

 answerOne.setText(randomizeOrderOfAnswers1);

Suppose that randomizeOrderOfAnswers1 is 'Y'.
So you can check right answer or not user choose.

boolean isAnswerCorrect = answerOne.getText().toString().equals("Y");

and compare after...

Remember - toString method always accessible to you. This is a method of Object class. It was always presented.