How to find the correct answer in a random generated array in an android quiz? -
i know how make button green when it's clicked if holds correct answer. don't know button holds correct answer because randomly generated. i've put in code when button clicked kind of sleeps second , moves new question button turns green time activity holds when new question generated turns in how was.
public class glavno extends activity implements onclicklistener { final handler handler = new handler(); int score = 0; int counter = 0; boolean ajme = true; textview textview1, textview2, textview3, countdown; button btn1, btn2, btn3, btn4; arraylist<question> qsts = new arraylist<question>(); list<integer> generated = new arraylist<integer>(); arraylist<string> allanswers = new arraylist<string>(); random rng = new random(); question nextquestion; question qjedan = new question( "q1", "correct answer - q1", "wrong answer 3 - q1", "wrong answer 3 - q1", "wrong answer 3 - q1" ); question q2 = new question( "q2", "correct answer - q2", "wrong answer 3 - q2", "wrong answer 3 - q2", "wrong answer 3 - q2" ); question q3 = new question( "q3", "correct answer - q3", "wrong answer 3 - q3", "wrong answer 3 - q3", "wrong answer 3 - q3" ); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.pitanja); new countdowntimer(20000, 1000) { public void ontick(long millisuntilfinished) { textview4.settext("seconds remaining: " + millisuntilfinished / 1000); } public void onfinish() { generatequestion(); textview2.settext("vrijeme!"); textview2.settextcolor(color.red); } }.start(); qsts.add(qjedan); qsts.add(q2); qsts.add(q3); textview1 = (textview) findviewbyid(r.id.textview1); textview2 = (textview) findviewbyid(r.id.textview2); textview3 = (textview) findviewbyid(r.id.textview3); textview3.settext("rezultat: " + score); btn1 = (button) findviewbyid(r.id.btn1); btn2 = (button) findviewbyid(r.id.btn2); btn3 = (button) findviewbyid(r.id.btn3); btn4 = (button) findviewbyid(r.id.btn4); btn1.setonclicklistener(this); btn2.setonclicklistener(this); btn3.setonclicklistener(this); btn4.setonclicklistener(this); generatequestion(); } public void generatequestion(){ while(ajme = true){ int nxt = rng.nextint(3); if (!generated.contains(nxt)){ generated.add(nxt); nextquestion = qsts.get(nxt); textview1.settext(nextquestion.questiontext); 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; } } } @override public void onclick(view v) { button b = (button)v; string buttontext = b.gettext().tostring(); if (counter == 3) { intent theintent = new intent(this, score.class); theintent.putextra("somename", score); startactivity(theintent); finish(); // added method call } else if(buttontext.equals(nextquestion.correctanswertext)) { counter++; textview3.settext("rezultat: " + (score += 10)); handler.postdelayed(new runnable() { @override public void run() { allanswers.clear(); generatequestion(); } }, 1000); return; } else{ counter++; textview2.settextcolor(color.red); handler.postdelayed(new runnable() { @override public void run() { allanswers.clear(); generatequestion(); } }, 1000); return; } }
use view
cast button
. have change declaration to:
final button b = (button)v;
now, if button holds correct answer, set background green. in handler, revert background original color:
else if(buttontext.equals(nextquestion.correctanswertext)) { counter++; textview3.settext("rezultat: " + (score += 10)); // set background color here b.setbackgroundcolor(color.green); handler.postdelayed(new runnable() { @override public void run() { // revert original color b.setbackgroundcolor(originalcolor); allanswers.clear(); generatequestion(); } }, 1000); return; }
Comments
Post a Comment