2013-04-19 149 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