title: how to increment or decrement scores on answer in android quiz?
body: <p>i am working on android quiz. i have 4 buttons for answers and text for these answers retriving from database. if correct answer is choosen i want score incremented and if wrong decremented. i have 20 fix rounds in quiz so whether the answer is wrong or right. i want every time round incremented. when we click answer its show answer right or wrong then proceed to next question.</p>
  
  <pre><code>questionactivity.class
  
  import java.util.List;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.KeyEvent;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  
  
  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 and get question
           */
          currentGame = ((CYKApplication)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 the question and answer options..
           */
          setQuestions();
  
      }
  
  
      /**
       * Method to set the text for the question and answers from the current games
       * current question
       */
      private void setQuestions() {
          //set the question text from current question
          String question = Utility.capitalise(currentQ.getQuestion());
          TextView qText = (TextView) findViewById(R.id.question);
          qText.setText(question);
  
          //set the available options
          List&lt;String&gt; 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)));
      }
  
  
      @Override
      public void onClick(View arg0) {
          //Log.d("Questions", "Moving to next question");
  
          if(!checkAnswer()) return;
  
          /**
           * check if end of game
           */
          if (currentGame.isGameOver()){
              //Log.d("Questions", "End of game! lets add up the scores..");
              //Log.d("Questions", "Questions Correct: " +            currentGame.getRight());
              //Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
              Intent i = new Intent(this, EndgameActivity.class);
              startActivity(i);
              finish();
          }
          else{
              Intent i = 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 a checkbox has been selected, and if it
       * has then check if its correct and update gamescore
       */
      private boolean checkAnswer() {
          String answer = getSelectedAnswer();
  
              if (currentQ.getAnswer().equalsIgnoreCase(answer))
              {
                  //Log.d("Questions", "Correct Answer!");
                  currentGame.incrementScore();
              }
              else{
                  //Log.d("Questions", "Incorrect Answer!");
                  currentGame.decrementScore();
              }
              return true;
          }
  
      /**
       * 
       */
      private String getSelectedAnswer() {
          Button c1 = (Button)findViewById(R.id.answer1);
          Button c2 = (Button)findViewById(R.id.answer2);
          Button c3 = (Button)findViewById(R.id.answer3);
          Button c4 = (Button)findViewById(R.id.answer4);
          if (c1.callOnClick())
          {
              return c1.getText().toString();
          }
          if (c2.callOnClick())
          {
              return c2.getText().toString();
          }
          if (c3.callOnClick())
          {
              return c3.getText().toString();
          }
          if (c4.callOnClick())
          {
              return c4.getText().toString();
          }
  
          return null;
      }
  
  }
  
  
  
  
  
  
  gameplay.java  
  
  import java.util.ArrayList;
  import java.util.List;
  
  
  public class GamePlay {
  
      private int numRounds;
      private int difficulty;
      private int score;
      private int round;
  
      private List&lt;Question&gt; questions = new ArrayList&lt;Question&gt;();
  
      /**
       * @return the right
       */
      public int getScore() {
          return score;
      }
      /**
       * @param right the right to set
       */
      public void setScore(int score) {
          this.score = score;
      }
  
      /**
       * @return the round
       */
      public int getRound() {
          return round;
      }
      /**
       * @param round the round to set
       */
      public void setRound(int round) {
          this.round = round;
      }
      /**
       * @param difficulty the difficulty to set
       */
      public void setDifficulty(int difficulty) {
          this.difficulty = difficulty;
      }
      /**
       * @return the difficulty
       */
      public int getDifficulty() {
          return difficulty;
      }
      /**
       * @param questions the questions to set
       */
      public void setQuestions(List&lt;Question&gt; questions) {
          this.questions = questions;
      }
  
      /**
       * @param q the question to add
       */
      public void addQuestions(Question q) {
          this.questions.add(q);
      }
  
      /**
       * @return the questions
       */
      public List&lt;Question&gt; getQuestions() {
          return questions;
      }
  
  
      public Question getNextQuestion(){
  
          //get the question
          Question next = questions.get(this.getRound());
          //update the round number to the next round
          this.setRound(this.getRound()+1);
          return next;
      }
  
      /**
       * method to increment the number of correct answers this game
       */
  
  
      /**
       * method to increment the number of incorrect answers this game
       */
      public void incrementScore(){
          score=score+100;
      }
  
      public void decrementScore()
      {
          score=score-50;
      }
      /**
       * @param numRounds the numRounds to set
       */
      public void setNumRounds(int numRounds) {
          this.numRounds = numRounds;
      }
      /**
       * @return the numRounds
       */
      public int getNumRounds() {
          return numRounds;
      }
  
      /**
       * method that checks if the game is over
       * @return boolean
       */
      public boolean isGameOver(){
          return (getRound() &gt;= getNumRounds());
      }
  
  
  }
  
  
  question.java
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.List;
  
  public class Question {
  
      private String question;
      private String answer;
      private String option1;
      private String option2;
      private String option3;
      private int rating;
  
  
      /**
       * @return the question
       */
      public String getQuestion() {
          return question;
      }
      /**
       * @param question the question to set
       */
      public void setQuestion(String question) {
          this.question = question;
      }
      /**
       * @return the answer
       */
      public String getAnswer() {
          return answer;
      }
      /**
       * @param answer the answer to set
       */
      public void setAnswer(String answer) {
          this.answer = answer;
      }
      /**
       * @return the rating
       */
      public int getRating() {
          return rating;
      }
      /**
       * @param rating the rating to set
       */
      public void setRating(int rating) {
          this.rating = rating;
      }
      /**
       * @return the option1
       */
      public String getOption1() {
          return option1;
      }
      /**
       * @param option1 the option1 to set
       */
      public void setOption1(String option1) {
          this.option1 = option1;
      }
      /**
       * @return the option2
       */
      public String getOption2() {
          return option2;
      }
      /**
       * @param option2 the option2 to set
       */
      public void setOption2(String option2) {
          this.option2 = option2;
      }
      /**
       * @return the option3
       */
      public String getOption3() {
          return option3;
      }
      /**
       * @param option3 the option3 to set
       */
      public void setOption3(String option3) {
          this.option3 = option3;
      }
  
      public List&lt;String&gt; getQuestionOptions(){
          List&lt;String&gt; shuffle = new ArrayList&lt;String&gt;();
          shuffle.add(answer);
          shuffle.add(option1);
          shuffle.add(option2);
          shuffle.add(option3);
          Collections.shuffle(shuffle);
          return shuffle;
      }
  
  }
  </code></pre>
  
  <p>This is my Log File:-</p>
  
  <pre><code> 09-02 08:17:30.083: E/Trace(2148): error opening trace file: No such file or directory (2)
      09-02 08:17:30.193: D/dalvikvm(2148): GC_FOR_ALLOC freed 65K, 8% free 2413K/2620K, paused 26ms, total 28ms
      09-02 08:17:30.203: I/dalvikvm-heap(2148): Grow heap (frag case) to 4.553MB for 2160016-byte allocation
      09-02 08:17:30.313: D/dalvikvm(2148): GC_FOR_ALLOC freed 1K, 5% free 4521K/4732K, paused 104ms, total 104ms
      09-02 08:17:30.363: D/dalvikvm(2148): GC_CONCURRENT freed &lt;1K, 5% free 4521K/4732K, paused 4ms+3ms, total 50ms
      09-02 08:17:30.832: D/gralloc_goldfish(2148): Emulator without GPU emulation detected.
      09-02 08:17:35.432: D/dalvikvm(2148): GC_FOR_ALLOC freed 17K, 4% free 4988K/5148K, paused 42ms, total 45ms
      09-02 08:17:35.482: I/dalvikvm-heap(2148): Grow heap (frag case) to 7.068MB for 2160016-byte allocation
      09-02 08:17:35.562: D/dalvikvm(2148): GC_CONCURRENT freed 4K, 3% free 7092K/7260K, paused 4ms+4ms, total 77ms
      09-02 08:17:36.512: I/Choreographer(2148): Skipped 764 frames!  The application may be doing too much work on its main thread.
      09-02 08:17:38.682: I/dalvikvm(2148): threadid=1: stack overflow on call to Landroid/view/View;.findViewById:LI
      09-02 08:17:38.682: I/dalvikvm(2148):   method requires 12+20+8=40 bytes, fp is 0x4205a320 (32 left)
      09-02 08:17:38.682: I/dalvikvm(2148):   expanding stack end (0x4205a300 to 0x4205a000)
      09-02 08:17:38.682: I/dalvikvm(2148): Shrank stack (to 0x4205a300, curFrame is 0x4205fec4)
      09-02 08:17:38.682: D/AndroidRuntime(2148): Shutting down VM
      09-02 08:17:38.682: W/dalvikvm(2148): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
      09-02 08:17:39.092: E/AndroidRuntime(2148): FATAL EXCEPTION: main
      09-02 08:17:39.092: E/AndroidRuntime(2148): java.lang.StackOverflowError
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.ViewGroup.findViewTraversal(ViewGroup.java:3053)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.findViewById(View.java:15104)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.Window.findViewById(Window.java:900)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.app.Activity.findViewById(Activity.java:1839)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:144)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.starchazer.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.View.callOnClick(View.java:4222)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.getSelectedAnswer(QuestionActivity.java:148)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.checkAnswer(QuestionActivity.java:126)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at com.cyk.QuestionActivity.onClick(QuestionActivity.java:85)
      09-02 08:17:39.092: E/AndroidRuntime(2148):     at android.view.V
      09-02 08:17:39.122: D/dalvikvm(2148): GC_CONCURRENT freed 762K, 11% free 7907K/8812K, paused 69ms+5ms, total 117ms
      09-02 08:17:39.122: D/dalvikvm(2148): WAIT_FOR_CONCURRENT_GC blocked 16ms
      09-02 08:22:39.222: I/Process(2148): Sending signal. PID: 2148 SIG: 9
      09-02 08:22:39.783: E/Trace(2182): error opening trace file: No such file or directory (2)
      09-02 08:22:39.883: D/dalvikvm(2182): GC_FOR_ALLOC freed 68K, 9% free 2413K/2624K, paused 25ms, total 27ms
      09-02 08:22:39.893: I/dalvikvm-heap(2182): Grow heap (frag case) to 4.553MB for 2160016-byte allocation
      09-02 08:22:40.013: D/dalvikvm(2182): GC_FOR_ALLOC freed 1K, 5% free 4521K/4736K, paused 114ms, total 114ms
      09-02 08:22:40.063: D/dalvikvm(2182): GC_CONCURRENT freed &lt;1K, 5% free 4521K/4736K, paused 4ms+3ms, total 49ms
      09-02 08:22:40.513: D/gralloc_goldfish(2182): Emulator without GPU emulation detected.
  </code></pre>
  
  <p>please someone help me by correcting my code. thanks in advance.</p>