2013-04-19 1 views
1

이 내 DateBaseHelper 공공 커서 getQuestionsFromQuizID (INT quizID) {안드로이드 임의의 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; 
      } 
    } 

그래서 ... 나는 무작위로 질문을 보여주기 위해 늘 때, 나는 10 을 제한하고 내 질문에 답변을 섞어 수) (RANDOM으로 정렬을 추가, SQL을 편집 ...

이 사람이 수 반복적 인 질문없이 임의의 질문을하는 법을 아십니까? 질문 및 답변의 데이터를 혼합하는 방법은 무엇입니까?

답변

1

sqlite 데이터에 사용하지 않는 경우 0을 사용하거나 사용하는 경우 1을 사용할 수 있습니다. 나는 그 질문이 이미 1에 사용되었다는 것을 표시 할 것입니다. 당신은 그 필드가 0 인 질문만을 반환하도록 데이터베이스를 질의 할 수 있습니다. 0으로 질문이 없으면 (모두 사용되었습니다) 해당 열을 0으로 설정하면 모든 질문을 다시 똑같이 사용할 수 있습니다.

여러 세션이 있고 시간이 지남에 따라 모든 질문에 응답해야하는 경우 도움이됩니다.

귀하의 질문과 같은 행에 답변을 저장한다고 가정합니다.

먼저 사용하지 않은 질문/답변 쌍을 하나씩 가져 오는 쿼리를 작성합니다. 그런 다음 사용 된 것으로 표시하십시오. 나는 그것을 "질문"으로 사용합니다. 그런 다음 3 ~ 4 개의 질문/응답 쌍에 대한 두 번째 쿼리를 수행하고 사용 여부에 대해 걱정하지 마십시오. 그 잘못된 대답을 가지고 객관식 질문에 대한 옳은 대답과 무작위로 섞어 라.

+0

답장을 보내지 마십시오. 질문과 답변의 데이터를 혼합하는 문제를 해결하는 방법을 알려주실 수 있습니까? –

+0

위의 수정 된 답변 이것에 대해 더 많은 질문을하는 것이 좋습니다, 나는 오늘 집에서 놀고 있습니다. – HalR

관련 문제