Java Android如何获取随机sqlite数据?

Java Android如何获取随机sqlite数据?,java,android,sqlite,Java,Android,Sqlite,这是我的约会助手 公共游标getQuestionsFromQuizID(int quizID){ …我有一个有方法的测验活动 私有void getNextQuestion(){ 字符串问题 // set question count text at the top of screen using string // concatenation Integer currentCount = new Integer(curQue

这是我的约会助手 公共游标getQuestionsFromQuizID(int quizID){

…我有一个有方法的测验活动

私有void 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,所有问题都同样可用

如果您正在进行多个会话,并且希望确保随着时间的推移,所有问题都会被击中,那么这将很有帮助

我假设你把答案和你的问题存储在同一行


我会先做一个查询,得到一对没有使用过的问题/答案。然后,当然,将其标记为已使用。我会将其作为“问题”。然后我会再查询3到4对问题/答案,不用担心它们是否被使用。将这些错误的答案与一个选择题的正确答案随机混合。

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

如果您正在进行多个会话,并且希望确保随着时间的推移,所有问题都会被击中,那么这将很有帮助

我假设你把答案和你的问题存储在同一行


我会先做一个查询,得到一对没有使用过的问题/答案。然后,当然,将其标记为已使用。我会将其作为“问题”。然后,我会再查询3到4对问题/答案,不必担心它们是否被使用。将这些错误的答案与多项选择题的正确答案随机混合。

t谢谢你的回答。你能告诉我如何解决问题,将问题和答案中的数据混合在一起吗?上面编辑的答案。请随意提出更多问题,我今天只是在家休息。谢谢你的回答。你能告诉我如何混合问答中的数据来解决问题吗?上面编辑的答案。请随意提出更多问题,我今天只是在家休息。
            // 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;
            }
    }