Well, I'm new in Java programming, and following a few tips that I've seen around here, I made this code to a quiz game:
public class OtherActivity extends Activity {
TextView textView1, textView2;
Button btn1, btn2, btn3, btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
final ArrayList<Question> qsts = new ArrayList<Question>();
qsts.add(Question.q1);
qsts.add(Question.q2);
qsts.add(Question.q3);
qsts.add(Question.q4);
qsts.add(Question.q5);
qsts.add(Question.q6);
final Random rng = new Random();
final List<Integer> generated = new ArrayList<Integer>();
int nxt = rng.nextInt(6);
generated.add(nxt);
final Question nextQuestion = qsts.get(nxt);
textView1.setText(nextQuestion.questionText);
final ArrayList<String> allAnswers = new ArrayList<String>();
allAnswers.add(nextQuestion.correctAnswerText);
allAnswers.add(nextQuestion.wrongAnswer1);
allAnswers.add(nextQuestion.wrongAnswer2);
allAnswers.add(nextQuestion.wrongAnswer3);
Collections.shuffle(allAnswers);
btn1.setText(allAnswers.get(0));
btn2.setText(allAnswers.get(1));
btn3.setText(allAnswers.get(2));
btn4.setText(allAnswers.get(3));
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(allAnswers.get(0) == nextQuestion.correctAnswerText){
textView2.setText("CORRECT ANSWER, MAN!");
while(true){
int nxt = rng.nextInt(6);
if (!generated.contains(nxt)){
generated.add(nxt);
Question nextQuestion = qsts.get(nxt);
textView1.setText(nextQuestion.questionText);
ArrayList<String> allAnswers = new ArrayList<String>();
allAnswers.add(nextQuestion.correctAnswerText);
allAnswers.add(nextQuestion.wrongAnswer1);
allAnswers.add(nextQuestion.wrongAnswer2);
allAnswers.add(nextQuestion.wrongAnswer3);
Collections.shuffle(allAnswers);
btn1.setText(allAnswers.get(0));
btn2.setText(allAnswers.get(1));
btn3.setText(allAnswers.get(2));
btn4.setText(allAnswers.get(3));
break;
}
}
}
else{
textView2.setText("WRONG ANSWER, MAN!");
while(true){
int nxt = rng.nextInt(6);
if (!generated.contains(nxt)){
generated.add(nxt);
// ----> Integer nxt = rng.nextInt(6); <---- random nxt aqui!
Question nextQuestion = qsts.get(nxt);
textView1.setText(nextQuestion.questionText);
ArrayList<String> allAnswers = new ArrayList<String>();
allAnswers.add(nextQuestion.correctAnswerText);
allAnswers.add(nextQuestion.wrongAnswer1);
allAnswers.add(nextQuestion.wrongAnswer2);
allAnswers.add(nextQuestion.wrongAnswer3);
Collections.shuffle(allAnswers);
btn1.setText(allAnswers.get(0));
btn2.setText(allAnswers.get(1));
btn3.setText(allAnswers.get(2));
btn4.setText(allAnswers.get(3));
break;
}
}
}
}
});
//
// AND THE SAME METHOD TO THE BUTTONS btn2, btn3, btn4
//
// btn2 with a if (allAnswers1.get(1) == nextQuestion.correctAnswerText) { ...
// btn3 with a if (allAnswers1.get(2) == nextQuestion.correctAnswerText) { ...
// btn4 with a if (allAnswers1.get(3) == nextQuestion.correctAnswerText) { ...
//
}
}
And I have this other class code:
public class Question {
String questionText;
String correctAnswerText;
String wrongAnswer1;
String wrongAnswer2;
String wrongAnswer3;
Question (String qst, String cAns, String wAns1, String wAns2, String wAns3){
questionText = qst;
correctAnswerText = cAns;
wrongAnswer1 = wAns1;
wrongAnswer2 = wAns2;
wrongAnswer3 = wAns3;
}
static Question q1 = new Question(
"Question 1",
"Correct answer - question 1",
"Wrong Answer 1 - question 1",
"Wrong Answer 2 - question 1",
"Wrong Answer 3 - question 1"
);
static Question q2 = new Question(
"Question 2",
"Correct answer - question 2",
"Wrong Answer 1 - question 2",
"Wrong Answer 2 - question 2",
"Wrong Answer 3 - question 2"
);
// ...
// and the same with q3, q4, q5 and q6
// ...
Well, how you can see, I'm having problems with the identification of the correct answer. In the first question it works fine, but not after this. I believe this problem happens because when I try to compare allAnswers.get(0) == nextQuestion.correctAnswerText
or with other index like (1), (2) or (3) of the ArrayList allAnswers, the "slot" selectioned for the comparation still have the first question text answer. So, what should I do?
*Plus: The code is getting really big, i tried to put an if
in the onClickListener and compare with the id
of the 4 buttons, but it had to be with other function, outside the onCreate, 'cause the ArrayList qsts (that holds the Question objects) is not declared and I can't compare the button clicked cause I must declare a different ArrayList. There's a better way to do what I want?