2013-02-11 45 views
1

每個瑣事遊戲結束後,將顯示Results頁面。大約20秒後,在你的時間用完之前,每個問題給你的時間是多長,然後轉到下一個問題,重新加載Results頁面。然後,當您進入Highscores頁面時,分數輸入了兩次。因此,我的QuestionView課程中有些東西被搞砸了,但是我找不到這個錯誤。一次在SQLite數據庫中輸入2個相同的記錄 - Android

如果需要更多信息,請告訴我。先進的謝謝!

Results.java

total_score = dh.calculateTotalScore(score, percentage); 
    if(dh.getLowest() == -1) { 
     dh.insert(score, percentage, total_score, category); 
    } else { 
     dh.delete(dh.getLowest()); 
     dh.insert(score, percentage, total_score, category); 
    } 

DatabaseHelper.java

public long insert(long score, int percentage, long total_score, String category) { 
    ContentValues values = new ContentValues(); 
    values.put(SCORE, score); 
    values.put(PERCENTAGE, percentage); 
    values.put(TOTAL_SCORE, total_score); 
    values.put(CATEGORY, category); 

    return db.insert(TABLE, null, values); 
} 

public void delete(long lowScore) { 
    lowScore = getLowest(); 
    db.delete(TABLE, TOTAL_SCORE + "=" + lowScore, null); 
} 

public long getLowest() { 
    Cursor c = db.rawQuery("SELECT * FROM " + TABLE + " ORDER BY " + TOTAL_SCORE, null); 
    long count = c.getCount(); 
    long lowScore = -1; 
    if(count == 10) { 
     c.moveToLast(); 
     lowScore = c.getInt(c.getColumnIndex(TOTAL_SCORE)); 
    } 
    return lowScore; 
} 

public long calculateTotalScore(long score, int percentage) { 
    long i; 
    return i = (percentage * 1000) + score; 
} 

QuestionView.java

public class QuestionView extends Activity { 

    int correctAnswers = 0; 
    int wrongAnswers = 0; 
    int answer = 0; 
    int i = 0; 

    long score = 0; 

    long startTime = 20000; 
    long interval = 1000; 
    long points; 

    boolean timerHasStarted = false; 

    String category; 

    Button answer1, answer2, answer3, answer4; 
    TextView question, pointCounter, questionNumber, timeCounter; 

    ArrayList<Question> queries; 
    Timer cdTimer; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.questionviewmain); 

     answer1 = (Button)findViewById(R.id.answer1); 
     answer2 = (Button)findViewById(R.id.answer2); 
     answer3 = (Button)findViewById(R.id.answer3); 
     answer4 = (Button)findViewById(R.id.answer4); 

     question = (TextView)findViewById(R.id.question); 

     category = getIntent().getStringExtra("category"); 
     queries = getIntent().getParcelableArrayListExtra("queries"); 

     pointCounter = (TextView)findViewById(R.id.timer); 
     questionNumber = (TextView)findViewById(R.id.timeElapsedView); 
     timeCounter = (TextView)findViewById(R.id.timeCounter); 

     cdTimer = new Timer(startTime, interval); 

     loadQuestion(); 
    } 

    public void loadQuestion() { 

     if(i == 10) { 

      endQuiz(); 

     } else { 

      if(!timerHasStarted) { 
       cdTimer.start(); 
       timerHasStarted = true; 
      } else { 
       cdTimer.start(); 
       timerHasStarted = false; 
      } 

      answer = queries.get(i).getCorrectAnswer(); 

      question.setText(queries.get(i).getQuery()); 

      answer1.setText(queries.get(i).getA1()); 
      answer2.setText(queries.get(i).getA2()); 
      answer3.setText(queries.get(i).getA3()); 
      answer4.setText(queries.get(i).getA4()); 

      answer1.setOnClickListener(new OnClickListener() { 
       public void onClick(View arg0) { 
        queries.get(i).setSelectedAnswer(0); 
        if(answer == 0) { 
         correctAnswers++; 
         nextQuestion(); 
        } else { 
         wrongAnswers++; 
         nextQuestion(); 
        } 
       } 
      }); 

      answer2.setOnClickListener(new OnClickListener() { 
       public void onClick(View arg0) { 
        queries.get(i).setSelectedAnswer(1); 
        if(answer == 1) { 
         correctAnswers++; 
         nextQuestion(); 
        } else { 
         wrongAnswers++; 
         nextQuestion(); 
        } 
       } 
      }); 

      answer3.setOnClickListener(new OnClickListener() { 
       public void onClick(View arg0) { 
        queries.get(i).setSelectedAnswer(2); 
        if(answer == 2) { 
         correctAnswers++; 
         nextQuestion(); 
        } else { 
         wrongAnswers++; 
         nextQuestion(); 
        } 
       } 
      }); 

      answer4.setOnClickListener(new OnClickListener() { 
       public void onClick(View arg0) { 
        queries.get(i).setSelectedAnswer(3); 
        if(answer == 3) { 
         correctAnswers++; 
         nextQuestion(); 
        } else { 
         wrongAnswers++; 
         nextQuestion(); 
        } 
       } 
      }); 
     } 
    } 

    public ArrayList<Question> getQueries() { 
     return queries; 
    } 

    public void nextQuestion() { 
     score = score + points; 
     i++; 
     loadQuestion(); 
    } 

    public class Timer extends CountDownTimer { 

     public Timer(long startTime, long interval) { 
      super(startTime, interval); 
     } 

     @Override 
     public void onFinish() { 
      if(i >= 9) { 
       cdTimer.cancel(); 
       endQuiz(); 
      } else { 
       wrongAnswers++; 
       nextQuestion(); 
      } 
     } 

     @Override 
     public void onTick(long millisUntilFinished) { 
      timeCounter.setText("Time remaining: " + (millisUntilFinished/100)); 
      points = (millisUntilFinished/100)/2; 
      pointCounter.setText("Points remaining: " + points); 
      if(i < 10) { 
       questionNumber.setText("Question " + (i + 1) + " of 10"); 
      } 
     } 
    } 

    public void endQuiz() { 
     Intent intent = new Intent(QuestionView.this, Results.class); 
     intent.putExtra("correctAnswers", correctAnswers); 
     intent.putExtra("wrongAnswers", wrongAnswers); 
     intent.putExtra("score", score); 
     intent.putParcelableArrayListExtra("queries", queries); 
     intent.putExtra("category", category); 
     startActivity(intent); 
    } 
} 

THE FIX:

更改:

if(i == 10) { 
     endQuiz(); 
    } 

......這樣:

if(i == 10) { 
     cdTimer.cancel(); 
     endQuiz(); 
    }** 
+0

如果你的數據庫有問題,它可能有助於顯示數據庫代碼。你在哪裏插入/查詢分數? – Geobits 2013-02-11 14:27:27

+0

我爲您添加了更多代碼,Geobits。 – Matt 2013-02-11 15:26:59

回答

2

大概endQuiz被調用兩次。只有當你在定時器啓動之前回答最後一個問題時。

因爲,當您回答最後一個問題時,我沒有看到您取消計時器。所以,即使你已經回答了,定時器仍然在滴答滴答,當它已經啓動時,它會調用endQuiz

如果您沒有回答最後一個問題,並且時間用完,那麼結果將只插入一次,因爲endQuiz將僅從計時器中被調用一次。

+0

它的作品感謝您的意見!檢查開幕帖子是否有正確的工作代碼。 – Matt 2013-02-11 19:47:48

相關問題