0

我正在使用 sqlite 数据库制作多个测验应用程序。

我在 rawquery 中使用“ORDER BY RANDOM()”随机选择 5 行,但同一行有时会连续出现。

在某些情况下,它显示 5 个不同的行。随机选择也能正常工作,所以我的 rawquery 似乎工作正常。

我只是不知道在哪种情况下它会连续显示相同的问题和选择。有没有人和我一样的问题??

这是我的代码。

public class QuizActivity extends Activity implements OnClickListener {

    int Correct = 0;
    int Cnt = 1;

    String fieldinfo;
    String question;
    String answer;
    String choice1;
    String choice2;

    private MySQLHelper mDbHelper;  
    private SQLiteDatabase db; 
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_quiz);
        onQuestion();
    }

    public void onQuestion(){
        mDbHelper = new MySQLHelper(this);
        db = mDbHelper.getWritableDatabase();  
        try{           
            Cursor c = db.rawQuery("SELECT field, question, choice1, choice2, answer FROM WineQuiz ORDER BY RANDOM() LIMIT 5", null);
            if(c.moveToFirst());
            {
            while(c.moveToNext()){
                fieldinfo = c.getString(c.getColumnIndex("field"));
                question = c.getString(c.getColumnIndex("question"));
                choice1 = c.getString(c.getColumnIndex("choice1"));
                choice2 = c.getString(c.getColumnIndex("choice2"));
                answer = c.getString(c.getColumnIndex("answer"));   
            };
            }
        }catch(Exception e){
            System.out.println("");;
        }finally{
            db.close();
        }        
            ImageView image3 = (ImageView) findViewById(R.id.imageChoice1);
            ImageView image4 = (ImageView) findViewById(R.id.imageChoice2);
            TextView textChoice1 = (TextView) findViewById(R.id.textChoice1);
            textChoice1.setText(choice1);
            TextView textChoice2 = (TextView) findViewById(R.id.textChoice2);
            textChoice2.setText(choice2);
            image3.setOnClickListener(this);
            image4.setOnClickListener(this);
    }

    public void onClick(View v){

        switch(v.getId()){
        case R.id.imageChoice1:
            if(answer.equals(choice1)){
                Correct++;
                Toast.makeText(this, "Correct", Toast.LENGTH_SHORT ).show();
            }else{
                Toast.makeText(this, "Incorrect", Toast.LENGTH_SHORT ).show();
            }
            break;
        case R.id.imageChoice2:
            if(choice2.equals(answer)){
                Correct++;
                Toast.makeText(this, "Correct", Toast.LENGTH_SHORT ).show();
            }else{
                Toast.makeText(this, "Incorrect", Toast.LENGTH_SHORT ).show();
            }
            break;
         }

        if(Cnt==5){
            Intent intent = new Intent(this, ResultActivity.class);
            intent.putExtra("Correct", Correct );
            startActivity(intent);
        }else{
            Cnt++;
            onQuestion();
        }
    }

}
4

1 回答 1

1

rawQuery调用返回一个带有五个问题的游标,但下面的循环会覆盖fieldinfo每次循环迭代中的变量(等),因此您最终只会得到五行中最后一行的值。(并且第一行被跳过,因为您moveToNext在移动到第一行之后才调用。)

每次调用查询都不能保证返回相同的五行,因此每次都会得到一些独立的随机行。onQuestion

您必须在开始时阅读所有五个问题(放入数组或类似的东西中),然后一一显示,而无需再次查询数据库。

于 2014-08-31T15:54:50.900 回答