I would be surprised if this code would compile and/or run correctly at all. You are trying to use the content of a String
as an variable name, cast that variable to an ArrayList<String>
and then to access the elements. This is wrong on so many levels that you should consider doing a few Java tutorials again.
If you do or feel that you can continue anyways, try this approach: You shouldn't store the questions and answers in a separate list, but together in a class.
class Question
{
//...
// maybe id and other stuff belonging to a question
//...
String questionText;
// separate because you need to tell the correct answer apart from the wrong ones later
// you could also just always use the first one in a set of answers.
String correctAnswerText;
ArrayList<String> wrongAnswerTexts;
}
Then you can store your questions in an ArrayList<Question>
in your app and set an answer as follows:
//...
// set up ArrayList<Question> questions here
//...
int nxt = rng.nextInt(6);
//...
// make sure your list is actually long enough for the generated index
//...
Question nextQuestion = questions.get(nxt);
//...
// make sure the retrieved object is valid
//...
// set the question text to nextQuestion.questionText;
//...
ArrayList<String> allAnswers = new ArrayList<String>();
allAnswers.add(nextQuestion.correctAnswerText);
allAnswers.addAll(nextQuestion.wrongAnswerTexts);
Collections.shuffle(allAnswers);
btn1.setText(allAnswers.get(0));
btn2.setText(allAnswers.get(1));
btn3.setText(allAnswers.get(2));
btn4.setText(allAnswers.get(3));