Check The Correct Answer
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.
Solution 1:
use Collections.shuffle(list)
to shuffle your answer array and then display the answer
Set 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
Here im displaying answers in buttons....
List<Integer> intList = new ArrayList<Integer>(Arrays.asList(0,1,2,3));
Collections.shuffle(intList);
Log.d("ERR","List after shuffling: " + intList);
// below answers will be assiagned randomly to buttons...
btn_cmpTagline[intList.get(0)].setText(answr1);
btn_cmpTagline[intList.get(0)].setTag("right");
btn_cmpTagline[intList.get(1)].setText(answr2);
btn_cmpTagline[intList.get(1)].setTag("wrong");
btn_cmpTagline[intList.get(2)].setText(answr3);
btn_cmpTagline[intList.get(2)].setTag("wrong");
btn_cmpTagline[intList.get(3)].setText(answr4);
btn_cmpTagline[intList.get(3)].setTag("wrong");
//On Click
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_tag1:
Log.d("ERR", v.getTag().toString());
if(v.getTag().toString().equalsIgnoreCase("right")){
//this button has right answer .. do anything
}
break;
case R.id.btn_tag2:
Log.d("ERR", v.getTag().toString());
if(v.getTag().toString().equalsIgnoreCase("right")){
//this button has right answer .. do anything
}
break;
case R.id.btn_tag3:
Log.d("ERR", v.getTag().toString());
if(v.getTag().toString().equalsIgnoreCase("right")){
//this button has right answer .. do anything
}
--
--
}
Solution 2:
When you remove items from the list the the index of the correct answer will change, so you have to update where the correct answer is located.
Solution 3:
you can get the value of the right answer instead of the index. then, as you add the answers to buttons you can check if it is the same with the correct answer and keep the new index.
Post a Comment for "Check The Correct Answer"