java - Display score in TextView in android? -


i working on android quiz. have setquestion() method in questionactivity class. have question.xml in create textview show scores. want write code in setquestion set textview of button score.basically scores retrieving getscore() method in gameplay class , scores updated right or wrong answer. in setquestion() method next question answer textview updated on next round.

public class gameplay {      private int numrounds;     private int difficulty;     private int score;     private int round;      private list<question> questions = new arraylist<question>();       /**      * @return right      */     public int getscore() {         return score;     }     /**      * @param right right set      */     public void setscore(int score) {         this.score = score;     }      /**      * @return round      */     public int getround() {         return round;     }     /**      * @param round round set      */     public void setround(int round) {         this.round = round;     }     /**      * @param difficulty difficulty set      */     public void setdifficulty(int difficulty) {         this.difficulty = difficulty;     }     /**      * @return difficulty      */     public int getdifficulty() {         return difficulty;     }     /**      * @param questions questions set      */     public void setquestions(list<question> questions) {         this.questions = questions;     }      /**      * @param q question add      */     public void addquestions(question q) {         this.questions.add(q);     }      /**      * @return questions      */     public list<question> getquestions() {         return questions;     }       public question getnextquestion(){          //get question         question next = questions.get(this.getround());         //update round number next round         this.setround(this.getround()+1);         return next;     }      /**      * method increment number of correct answers game      */       /**      * method increment number of incorrect answers game      */     public void incrementscore(){         score=score+100;     }      public void decrementscore()     {         score=score-50;     }     /**      * @param numrounds numrounds set      */     public void setnumrounds(int numrounds) {         this.numrounds = numrounds;     }     /**      * @return numrounds      */     public int getnumrounds() {         return numrounds;     }      /**      * method checks if game on      * @return boolean      */     public boolean isgameover(){         return (getround() >= getnumrounds());     }   }        public class questionactivity extends activity implements onclicklistener{      private question currentq;     private gameplay currentgame;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.question);         /**          * configure current game , question          */         currentgame = ((abcapplication)getapplication()).getcurrentgame();         currentq = currentgame.getnextquestion();         button nextbtn1 = (button) findviewbyid(r.id.answer1);         nextbtn1.setonclicklistener(this);         button nextbtn2 = (button) findviewbyid(r.id.answer2);         nextbtn2.setonclicklistener(this);         button nextbtn3 = (button) findviewbyid(r.id.answer3);         nextbtn3.setonclicklistener(this);         button nextbtn4 = (button) findviewbyid(r.id.answer4);         nextbtn4.setonclicklistener(this);         /**          * update question , answer options..          */         setquestions();      }       /**      * method set text question , answers current games      * current question      */     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();         textview option1 = (textview) findviewbyid(r.id.answer1);         option1.settext(utility.capitalise(answers.get(0)));          textview option2 = (textview) findviewbyid(r.id.answer2);         option2.settext(utility.capitalise(answers.get(1)));          textview option3 = (textview) findviewbyid(r.id.answer3);         option3.settext(utility.capitalise(answers.get(2)));          textview option4 = (textview) findviewbyid(r.id.answer4);         option4.settext(utility.capitalise(answers.get(3)));          textview score1=(textview) findviewbyid(r.id.score);         settext score1=gameplay.getscore();            }       @override     public void onclick(view arg0) {         //log.d("questions", "moving next question");     if(!checkanswer(arg0)) return;            /**          * check if end of game          */         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();         }     }         @override     public boolean onkeydown(int keycode, keyevent event)     {         switch (keycode)         {         case keyevent.keycode_back :             return true;         }          return super.onkeydown(keycode, event);     }       /**      * check if checkbox has been selected, , if      * has check if correct , update gamescore      */     private boolean checkanswer(view v) {          button b=(button) v;         string answer = b.gettext().tostring();              //log.d("questions", "valid checkbox selection made - check if correct");             if (currentq.getanswer().equalsignorecase(answer))             {                 //log.d("questions", "correct answer!");                 currentgame.incrementscore();             }             else{                 //log.d("questions", "incorrect answer!");                 currentgame.decrementscore();             }             return true;         }         question.xml   <textview         android:id="@+id/score"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="@drawable/score_img"/> 

you should check this:

 int score = currentgame.getscore(); ; 

and convert string

 string scr = string.valueof(score); 

then set textview

 textview score1=(textview) findviewbyid(r.id.score);  score1.settext(scr);   

ok..


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -