Want to display correct answer and player selected answer at the end of android quiz game -
i have quiz app consists of question & 4 buttons options. @ end of quiz, app displays correct answers questions. want display correct answer , player selected answer @ end of quiz. here's code working display answers.
public static string getanswers(list<question> questions) { int question = 1; stringbuffer sb = new stringbuffer(); (question q : questions){ sb.append("q").append(question).append(") ").append(q.getquestion()).append("? \n"); sb.append("answer: ").append(q.getanswer()).append("\n\n"); question ++; } return sb.tostring();}
and quesactivity is:
private void setquestions() { //set question text current question string question = utility.capitalise(currentq.getquestion()) + "?"; textview qtext = (textview) findviewbyid(r.id.question); qtext.settext(question); //set available options list<string> answers = currentq.getquestionoptions(); btn1 = (button) findviewbyid(r.id.button1); btn1.settext(utility.capitalise(answers.get(0))); btn1.setonclicklistener(this); btn2 = (button) findviewbyid(r.id.button2); btn2.settext(utility.capitalise(answers.get(1))); btn2.setonclicklistener(this); btn3 = (button) findviewbyid(r.id.button3); btn3.settext(utility.capitalise(answers.get(2))); btn3.setonclicklistener(this); btn4 = (button) findviewbyid(r.id.button4); btn4.settext(utility.capitalise(answers.get(3))); btn4.setonclicklistener(this); } @override public void onclick(view arg0) { if (!checkanswer()) return; if (currentgame.isgameover()){ log.d("questions", "end of game! lets add scores.."); log.d("questions", "questions correct: " + currentgame.getright()); log.d("questions", "questions wrong: " + currentgame.getwrong()); intent = new intent(this, endgameactivity.class); startactivity(i); finish(); } else{ intent = new intent(this, questionactivity.class); startactivity(i); finish(); } } private boolean checkanswer() { string answer = getselectedanswer(); log.d("yourans", currentq.getanswer()+" "+answer); if (currentq.getanswer().equalsignorecase(answer)) { log.d("questions", "correct answer!"); currentgame.incrementrightanswers(); } else{ log.d("questions", "incorrect answer!"); currentgame.incrementwronganswers(); } return true; } private string getselectedanswer() { button b1 = (button)findviewbyid(r.id.button1); button b2 = (button)findviewbyid(r.id.button2); button b3 = (button)findviewbyid(r.id.button3); button b4 = (button)findviewbyid(r.id.button4); if (b1.ispressed()) { return b1.gettext().tostring(); } if (b2.ispressed()) { return b2.gettext().tostring(); } if (b3.ispressed()) { return b3.gettext().tostring(); } if (b4.ispressed()) { return b4.gettext().tostring(); } return null; }}
add property in class question named, useranswer. 1 field correct answer , 1 user answer. there propety iscorrect. have class named question. that, ignoring syntax.
class question { string mquestion; string moption1; string moption2; string moption3; string moption4; string mcorrectanswer; string museranswer; string mstatusofquestion (asked/not asked yet) }
you set questions in way doing right now. set class objects , put them in arraylist.
arraylist<question> mquestionslist = new arraylist<question> setquestion() { question mquestion = new question(); string question = utility.capitalise(currentq.getquestion()) + "?"; textview qtext = (textview) findviewbyid(r.id.question); qtext.settext(question); mquestion.setquestion(question); //set available options list<string> answers = currentq.getquestionoptions(); btn1 = (button) findviewbyid(r.id.button1); btn1.settext(utility.capitalise(answers.get(0))); mquestion.setoption1(answers.get(0)); btn1.setonclicklistener(this); btn2 = (button) findviewbyid(r.id.button2); btn2.settext(utility.capitalise(answers.get(1))); mquestion.setoption2(answers.get(1)); btn2.setonclicklistener(this); btn3 = (button) findviewbyid(r.id.button3); btn3.settext(utility.capitalise(answers.get(2))); btn3.setonclicklistener(this); mquestion.setoption3(answers.get(2)); btn4 = (button) findviewbyid(r.id.button4); btn4.settext(utility.capitalise(answers.get(3))); btn4.setonclicklistener(this); mquestion.setoption4(answers.get(3)); mquestion.setcorrectanswer(corectanswer); }
when user taps on option set value of object of question like
onuserclick(int position 2) { mquestionslist.get(position).setuseranswer(tappedoption); if(tappedoption == mquestionslist.get(position).getcorrectanswer()) iscorrect = true; else iscorrect = false; }
Comments
Post a Comment