如何在测验应用程序中随机化问题而不重复并且没有退出第一个错误的答案?

时间:2016-11-01 20:36:38

标签: android

这是我第一次在这里发布任何内容,我对格式化不太好,这篇文章也不完美。我真的需要帮助。我是一个菜鸟。

getannswer功能是根据用户输入控制事件的地方。

public void getAnswer(String AnswerString) {
    if (this.ans.equals(AnswerString)) {

        // if conditions matches increase the int (score) by 1
        // and set the text of the score view
        this.score++;
        this.scored.setText("" + this.score);
        Toast.makeText(getApplicationContext(), "CORRECT!", Toast.LENGTH_SHORT).show();
        db.getQuestion();
    } else {

        // if the answer is wrong start activity and finish the test

        Intent intent = new Intent(Testactivity.this, result.class);
        Toast.makeText(getApplicationContext(), "WRONG, SORRY!", Toast.LENGTH_SHORT).show();
        db.getScore(test_entry.username, this.score);

        // passing the int value
        Bundle b = new Bundle();
        b.putInt("score", score); // Your score
        intent.putExtras(b); // Put your score to your next
        startActivity(intent);
        finish();
    }

}

getquestion是我试图实现从数据库中获取问题的方法。我的问题是我不能让它跳过它已经采取的问题。正如你所看到的,我试图在一些问题中使用图像,但这并没有很好地解决问题,而不是问题的重点。

public void getQuestion() {
    Random r = new Random();
    int n = r.nextInt(20);
    db = this.getReadableDatabase();
    ctr++;
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_QUEST + " WHERE `qid` = " + n + ";", null);
    if (cursor.moveToFirst()) {
        if(cursor.getInt(6) == 1) {
            ta.txtQuestion.setVisibility(View.VISIBLE);
            ta.txtQuestion.setText(cursor.getString(1).toString());
            /*Log.e("IMAGE", "I WAS HERE");
            ta.img.setVisibility(View.VISIBLE);
            int imageResource = draw.getResources().getIdentifier("@drawable/"+cursor.getString(1), null, PACKAGE);
            Drawable res = draw.getResources().getDrawable(imageResource);
            ta.img.setImageDrawable(res);
            //ta.img.setImageResource(imageResource);*/
        } else {
            ta.txtQuestion.setVisibility(View.VISIBLE);
            ta.txtQuestion.setText(cursor.getString(1).toString());
        }
        ta.button1.setText(cursor.getString(3).toString());
        ta.button2.setText(cursor.getString(4).toString());
        ta.button3.setText(cursor.getString(5).toString());
        ta.ans = cursor.getString(2).toString();
        Log.e("Current Question ID", Integer.toString(n));
        Testactivity.txtCounter.setText(Integer.toString(ctr));
    }

如果我遗漏了任何相关的代码或说明..请告诉我,以便我可以更新此帖子。

public List<Question> getAllQuestions() {
    List<Question> quesList = new ArrayList<Question>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
    dbase = this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
           // Question quest = new Question();
            //quest.setID(cursor.getInt(0));
            Question quest = new Question();
            quest.setID(cursor.getInt(0));
            quest.setQUESTION(cursor.getString(1));
            quest.setANSWER(cursor.getString(5));
            quest.setOPTA(cursor.getString(2));
            quest.setOPTB(cursor.getString(3));
            quest.setOPTC(cursor.getString(4));

            quesList.add(quest);
        } while (cursor.moveToNext());
    }
    // return quest list
    return quesList;
}

上面的一个是从数据库中获取问题的新函数。然而。我无法使用NEWID()函数,该函数应该返回具有随机生成的ID的行。或者也许我的理解是错误的。

1 个答案:

答案 0 :(得分:0)

您应该检索所有问题,将它们存储在您想要的任何Collection中,然后转到getQuestion中的下一个问题。

当您从数据库中检索问题时,您可以告诉SQL返回random order

如果因为任何原因而改变的SO答案在这里已经改变了答案的相关部分:

SELECT * FROM table
ORDER BY RANDOM()

因此,您要从查询中删除WHERE子句,因为您现在想要检索所有问题,但ORDER BY NEWID()会以随机顺序返回它们。