2013-04-19 147 views
1

這是我DateBaseHelper 公共光標getQuestionsFromQuizID(INT quizID){Android如何獲得隨機sqlite數據?

   Integer quiz = Integer.valueOf(quizID); 

      return myDataBase.rawQuery("select * from Questions where quizID ="+quiz.toString(), null); 

    } 

    public Cursor getAnswersFromQuestionID(int questionID, int quizID){ 

      Integer question = Integer.valueOf(questionID); 
      Integer quiz = Integer.valueOf(quizID); 

      return myDataBase.rawQuery("select * from Answers where questionID = "+question.toString()+" and quizID = "+quiz.toString(), null); 
    } 

    public Boolean isAnswerCorrect(int answerID) { 

      Integer answer = Integer.valueOf(answerID); 
      int correctBit; 

      Cursor cursor = myDataBase.rawQuery("select * from Answers where _id =" 
          + answer.toString(), null); 

      cursor.moveToFirst(); 
      correctBit = cursor.getInt(3); 

      if (correctBit == 1) { 
        return true; 
      } else { 
        return false; 
      } 

    } 

    public int getQuestionCountByQuizID(int quizID){ 

      Integer quiz = Integer.valueOf(quizID); 

      Cursor cursor = myDataBase.rawQuery("select * from Questions where quizID ="+quiz.toString(), null); 

      cursor.moveToFirst(); 

      return cursor.getCount(); 

    } 

......我有方法)競猜活動

私人無效getNextQuestion({ 字符串的問題;

  // set question count text at the top of screen using string 
      // concatenation 
      Integer currentCount = new Integer(curQuestion + 1); 
      ((TextView) findViewById(R.id.questioncount)).setText("Question " 
          + currentCount.toString() + " of " 
          + totalQuestionCount.toString()); 

      // get quizID based on which button was pressed on PickQuizActivity 
      int quizID = getIntent().getExtras().getInt("quizID"); 

      // use returned cursor to get question as string 
      Cursor questionCursor = myDbHelper.getQuestionsFromQuizID(quizID); 

      questionCursor.moveToPosition(curQuestion); 

      // getString(3): gets the zero-indexed column 2 from current row cursor 
      // is on 
      question = questionCursor.getString(2); 

      // update UI to show question pulled from database 
      ((TextView) findViewById(R.id.question)).setText(question); 

      Cursor answerCursor = myDbHelper.getAnswersFromQuestionID(
          curQuestion + 1, quizID); 

      // set each answer text, there will always be 4 answers 
      // also collect questionID for isCorrect check when onClick is called 
      answerCursor.moveToFirst(); 
      ((TextView) findViewById(R.id.answer1)).setText(answerCursor 
          .getString(4)); 
      answerID1 = answerCursor.getInt(0); 

      answerCursor.moveToNext(); 
      ((TextView) findViewById(R.id.answer2)).setText(answerCursor 
          .getString(4)); 
      answerID2 = answerCursor.getInt(0); 

      answerCursor.moveToNext(); 
      ((TextView) findViewById(R.id.answer3)).setText(answerCursor 
          .getString(4)); 
      answerID3 = answerCursor.getInt(0); 

      answerCursor.moveToNext(); 
      ((TextView) findViewById(R.id.answer4)).setText(answerCursor 
          .getString(4)); 
      answerID4 = answerCursor.getInt(0); 

      // increment question counter, will be used to retrieve next question 
      curQuestion++; 

      // set flag for last question reached 
      if (questionCursor.isLast()) { 
        lastQuestionReached = true; 
      } 
    } 

所以......當我不會表現出隨機的問題,我編輯SQL,只需添加排序隨機的()LIMIT 10 和我的問題得到的答案混合起來......

任何人可以知道解決方案如何獲得隨機問題而不重複? 如何避免混合問題和答案中的數據?

回答

1

你可以在sqlite數據中有一個額外的列,如果不使用的話它可以是0,如果使用的話可以是1。我將標記該問題已用於1.您可以查詢數據庫僅返回該字段爲0的問題。如果沒有問題可用0(它們都已被使用),只需重置所有值該列爲0,所有問題都可以重複使用。

如果您有多個會話,並且希望確保隨着時間的推移,所有問題都能得到解決,這將有所幫助。

我假設你將你的答案存儲在與你的問題相同的行上。

我會先做一個查詢,得到1個問題/答案對還沒有被使用過。然後,當然,將其標記爲已使用。我會用它作爲「問題」。然後,我會對第三個或第四個問題/答案對進行第二個查詢,如果它們被使用或不使用,請不要擔心。採取那些錯誤的答案,並隨機地將它們與正確的答案混合成一個多項選擇題。

+0

打開你的答案。你能告訴我如何通過混合問題和答案的數據來解決問題嗎? –

+0

上面編輯的答案。隨意就此提出更多問題,今天我只是在家裏放鬆一下。 – HalR