android - How to move to a new activity after the finishing the quiz? -
i'm making android quiz app , seems it's done except adding questions there still big error. see i've put random generater generates 4 questions. when answer fourth new activity starts display of score. instead app doesn't respond. i'm clueless.
thank you!
package com.matej.hajdukkviz; import java.util.arraylist; import java.util.collections; import java.util.list; import java.util.random; import android.os.bundle; import android.app.activity; import android.graphics.color; import android.view.view; import android.view.window; import android.view.windowmanager; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; public class glavno extends activity implements onclicklistener { int score = 0; 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 q1 = new question( "q1", "correct answer - q1", "wrong answer 1 - q1", "wrong answer 2 - q1", "wrong answer 3 - q1" ); question q2 = new question( "q2?", "correct answer - q2", "wrong answer 1 - q2", "wrong answer 2 - q2", "wrong answer 3 - q2" ); question q3 = new question( "q3?", "correct answer - q3" "wrong answer 1 - q3", "wrong answer 2 - q3", "wrong answer 3 - q3" ); question q4 = new question( "q4?", "correct answer - q4", "wrong answer 1 - q4", "wrong answer 2 - q4", "wrong answer 3 - q4" ); @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); // add questions in arraylist qsts qsts.add(q1); qsts.add(q2); qsts.add(q3); qsts.add(q4); textview1 = (textview) findviewbyid(r.id.textview1); textview2 = (textview) findviewbyid(r.id.textview2); textview3 = (textview) findviewbyid(r.id.textview3); countdown = (textview) findviewbyid(r.id.countdown); 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(true){ int nxt = rng.nextint(4); 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(buttontext.equals(nextquestion.correctanswertext)) { textview2.settext("toČno!"); textview2.settextcolor(color.green); textview3.settext("rezultat: " + (score += 10)); allanswers.clear(); generatequestion(); return; }else{ textview2.settext("netoČno!"); textview2.settextcolor(color.red); textview3.settext("rezultat: " + (score -= 5)); allanswers.clear(); generatequestion(); return; } }
}
in generatequestion()
looping while true
(which case) , execute if random question has not been shown. of course endless loop. if 4 questions, can declare global variable counter
increase after each question. , instead of looping while true
if counter < 4
else startnewintent()
or similar code mentioned codemagic.
Comments
Post a Comment