java - Track score in an android quiz app -
i want register correctanswers every question that's answered, moves next question after 1 of alternatives clicked. want display amount of correct answers on xml file contains textview that's going hold score right after last question answered
i have picture of flag , textview question 4 buttons beneath alternatives.
i think have gone through entire internet, have been stuck days. appreciate help.:)
if there unclear, please feel free ask.
this question class holds question, pictureid (for country flags in quiz), option a, b, c , correct answer. holds score.
public class question { private int bildid; private string question; private string opta; private string optb; private string optc; private string answer; private string score; //constructor public question(int bildid, string question, string opta, string optb, string optc, string answer) { this.bildid = bildid; this.question = question; this.opta = opta; this.optb = optb; this.optc = optc; this.answer = answer; } //end konstruktor //get & set method starts public int getbildid() { return bildid; } public void setbildid(int bildid) { this.bildid = bildid; } public string getquestion() { return question; } public void setquestion(string question) { this.question = question; } public string getopta() { return opta; } public void setopta(string opta) { this.opta = opta; } public string getoptb() { return optb; } public void setoptb(string optb) { this.optb = optb; } public string getoptc() { return optc; } public void setoptc(string optc) { this.optc = optc; } public string getanswer() { return answer; } public void setanswer(string answer) { this.answer = answer; } public string getscore() { return score; } public void setscore(string score) { this.score = score; } //get & set methods end } //end konstruktor
here mainactivity.
public class mainactivity extends appcompatactivity { //method that's gonna check index postion , avoid outofbounds. private void fixindexcountvalue() { if (idx >= questionarray.size() - 1) { idx = questionarray.size() - 1; } else if (idx < 0) { idx = 0; } } //creates arraylist thats gonna hold questions, options, answers, pictureid. arraylist<question> questionarray = new arraylist<>(); //arraylist thats gonna hold buttons arraylist<button> buttons = new arraylist<>(); //variable index postion int idx = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //arraylists questionarray = new arraylist<>(); buttons = new arraylist<>(); //end //code questions question q1 = new question(r.drawable.australien, "what capital of australia?", "melbourne", "chicago", "sydney", "canberra"); question q2 = new question(r.drawable.finland, "what country flag belong to?", "sweden", "denmark", "norway", "finland"); question q3 = new question(r.drawable.frankrike, "what biggest city in france?", "lyon", "marseille", "bordeaux", "paris"); question q4 = new question(r.drawable.jamaika, "what country flag belong to?", "mexico", "dominican republic", "cuba", "jamaica"); question q5 = new question(r.drawable.kina, "what capital of china?", "shanghai", "hangzhou", "guangzhou", "beijing"); question q6 = new question(r.drawable.kurdistan, "what capital of kurdistan?", "kirkuk", "dohuk", "zakho", "erbil"); question q7 = new question(r.drawable.sverige, "what capital of sweden?", "gothenburg", "malmö", "helsinki", "stockholm"); question q8 = new question(r.drawable.norge, "what capital of norway?", "bergen", "harstad", "stockholm", "oslo"); question q9 = new question(r.drawable.ryssland, "what city in russia has highest population?", "saint petersburg", "omsk", "samara", "moskva"); question q10 = new question(r.drawable.tyskland, "what country flag belong to?", "belgia", "italy", "austria", "germany"); questionarray.add(q1); questionarray.add(q2); questionarray.add(q3); questionarray.add(q4); questionarray.add(q5); questionarray.add(q6); questionarray.add(q7); questionarray.add(q8); questionarray.add(q9); questionarray.add(q10); final question question = questionarray.get(idx); //end code questions // code that's gonna fill buttons, tv , imageview data. final imageview bild = (imageview) findviewbyid(r.id.imageview); final textview tv_question = (textview) findviewbyid(r.id.tv_question); final button btnalt1 = (button) findviewbyid(r.id.btnalt1); final button btnalt2 = (button) findviewbyid(r.id.btnalt2); final button btnalt3 = (button) findviewbyid(r.id.btnalt3); final button btnalt4 = (button) findviewbyid(r.id.btnalt4); //textview on scorescreen thats going display score final textview tvscore = (textview) findviewbyid(r.id.tvscore); //knapp som kommer upp pÅ fÖrsta sidan, starta quiz final button btnstart = (button) findviewbyid(r.id.btnstart); //adds data question class methods tv_question.settext(question.getquestion()); bild.setimageresource(question.getbildid()); btnalt1.settext(question.getopta()); btnalt2.settext(question.getoptb()); btnalt3.settext(question.getoptc()); btnalt4.settext(question.getanswer()); //end //adds button id in arraylist buttons.add(btnalt1); buttons.add(btnalt2); buttons.add(btnalt3); buttons.add(btnalt4); //end //code click function on each button btnalt1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { idx++; fixindexcountvalue(); question questionclick = questionarray.get(idx); tv_question.settext(questionclick.getquestion()); bild.setimageresource(questionclick.getbildid()); collections.shuffle(buttons); buttons.get(0).settext(questionclick.getopta()); buttons.get(1).settext(questionclick.getoptb()); buttons.get(2).settext(questionclick.getoptc()); buttons.get(3).settext(questionclick.getanswer()); } }); //end button 1 btnalt2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { idx++; fixindexcountvalue(); question questionclick = questionarray.get(idx); tv_question.settext(questionclick.getquestion()); bild.setimageresource(questionclick.getbildid()); collections.shuffle(buttons); buttons.get(0).settext(questionclick.getopta()); buttons.get(1).settext(questionclick.getoptb()); buttons.get(2).settext(questionclick.getoptc()); buttons.get(3).settext(questionclick.getanswer()); } }); //end button 2 btnalt3.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { idx++; fixindexcountvalue(); question questionclick = questionarray.get(idx); tv_question.settext(questionclick.getquestion()); bild.setimageresource(questionclick.getbildid()); collections.shuffle(buttons); buttons.get(0).settext(questionclick.getopta()); buttons.get(1).settext(questionclick.getoptb()); buttons.get(2).settext(questionclick.getoptc()); buttons.get(3).settext(questionclick.getanswer()); } }); //end button 3 btnalt4.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { idx++; fixindexcountvalue(); question questionclick = questionarray.get(idx); tv_question.settext(questionclick.getquestion()); bild.setimageresource(questionclick.getbildid()); collections.shuffle(buttons); buttons.get(0).settext(questionclick.getopta()); buttons.get(1).settext(questionclick.getoptb()); buttons.get(2).settext(questionclick.getoptc()); buttons.get(3).settext(questionclick.getanswer()); } });//end button 4 } //end oncreate } //end main
download updated project here
i have added method in mainactivity :
private void shownextquestionandsaveanswer(string answer) { if (answer == null) return; if (questionarray.get(idx).getanswer().equalsignorecase(answer)) { questionarray.get(idx).iscorrect = true; } idx++; if (idx < questionarray.size()) { fixindexcountvalue(); question questionclick = questionarray.get(idx); tv_question.settext(questionclick.getquestion()); bild.setimageresource(questionclick.getbildid()); collections.shuffle(buttons); buttons.get(0).settext(questionclick.getopta()); buttons.get(1).settext(questionclick.getoptb()); buttons.get(2).settext(questionclick.getoptc()); buttons.get(3).settext(questionclick.getanswer()); } else { intent resultintent = new intent(mainactivity.this,resultactivity.class); resultintent.putextra("list",questionarray); startactivity(resultintent); } }
Comments
Post a Comment