I am developing an app that is a question’s game. I managed to show the answers to each question to a different button each time but i am having trouble to check the right answer. My approach is: I created an extra column to the database where i indicate the column where the right answer is(1,2,3 or 4). I use this code for showing the answers in different buttons.
cur = dbHelper.getRandomQuestion();
String corrans = cur.getString(cur.getColumnIndex("CorrectAnswer"));
a = Integer.parseInt(corrans);
String question = cur.getString(cur.getColumnIndex("QUESTIONS"));
String answer0 = cur.getString(cur.getColumnIndex("ANSWER1"));
String answer1 = cur.getString(cur.getColumnIndex("ANSWER2"));
String answer2 = cur.getString(cur.getColumnIndex("ANSWER3"));
String answer3 = cur.getString(cur.getColumnIndex("ANSWER4"));
txtQuest.setText(question);
ArrayList<String> lstAnswers = new ArrayList<String>();
lstAnswers.add(answer0);
lstAnswers.add(answer1);
lstAnswers.add(answer2);
lstAnswers.add(answer3);
score.setText("Your score is " + b +","+ a);
Random random = new Random();
int[] textViews = new int[] { R.id.button1, R.id.button2, R.id.button3, R.id.button4 };
int textViewIndex = 0;
while (lstAnswers.size() > 0) {
int index = random.nextInt(lstAnswers.size());
if(a == index){ b = index;}
else{}
String randomAnswer = lstAnswers.remove(index);
((TextView)findViewById(textViews[textViewIndex])).setText(randomAnswer);
++textViewIndex;
}
To each button call to compare the values of a and b and then act accordingly. But it doesnt seem to work. I understand why but i cannot figure it out. Any help appeciated.
use
Collections.shuffle(list)to shuffle your answer array and then display the answerSet a tag as “right” to your button where anwer is correct and later compare the the tagvalue and disply right or wrong
EDIT: below thing is just an outline
//On Click