java - Android如何获取随机sqlite数据?

标签 java android sqlite

这是我的 DateBaseHelper public Cursor getQuestionsFromQuizID(int quizID){

             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();

    }

...我有方法测验 Activity

私有(private)无效 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,只需添加 SORT BY RANDOM() LIMIT 10 我的问题和答案混在一起...

谁能知道如何在不重复的情况下获得随机问题的解决方案? 以及如何避免混合问题和答案的数据?

最佳答案

您可以在 sqlite 数据中有一个额外的列,如果不使用则为 0,如果使用则为 1。我会用 1 标记该问题已被使用。您可以查询数据库以仅返回该字段为 0 的问题。如果 0 没有可用的问题(它们都已被使用),只需重置所有值该列为 0,所有问题再次同样可用。

如果您有多个 session ,并且希望确保随着时间的推移,所有问题都得到解决,这将有所帮助。

我假设您将答案存储在与问题相同的行中。

我会首先进行查询以获取 1 个未使用的问题/答案对。然后,当然,将其标记为已使用。我会用它作为“问题”。然后我会再查询 3 或 4 个问题/答案对,并且不用担心它们是否被使用。把那些错误的答案随机地与正确答案混合起来做一道选择题。

关于java - Android如何获取随机sqlite数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16095619/

相关文章:

java - 空构造函数模块和在 Dagger 2 中提供带有参数的依赖项的模块有什么区别?

sql - Android Sqlite语句问题

java - 在 GSON 反序列化期间识别无效字段值

java - Android - Log 类型未定义方法 i(String, String)

java - 使用 WindowManager 覆盖页脚

sqlite - 删除几乎重复的行

android - 创建包含主键和 4 个外键的 SQLite 表时出错

java - 如何将按钮分配给 Android 中的 Activity ?

javascript - Spring Controller URL 请求映射仅在带有字符串的空白页面上不起作用

Android Studio 1.2.1.1 - 找不到样式 'textViewStyle'