无法从数据库中获取随机行以在主要活动上显示为Q& A.

时间:2012-09-03 05:59:10

标签: android sqlite random

这种方法是否正确?

public List<Question> getQuestionSet(int i){
        List<Question> questionSet = new ArrayList<Question>();
       Cursor c = db.rawQuery("SELECT * FROM quiztable ORDER BY RANDOM() LIMIT X" + i, null);
        while (c.moveToNext()){
            Question q = new Question();
            q.setQuestion(c.getString(1));
            q.setAnswer(c.getString(2));
            q.setOption1(c.getString(3));
            q.setOption2(c.getString(4));
            questionSet.add(q);
        }
        return questionSet;

如果是,我如何创建一种方法,以随机顺序逐个显示我的数据库中的每个问题..抱歉。我不知道该怎么做......

编辑: 我尝试将上面的代码更改为您的代码。现在另一个问题。这是我在MainActivity文件中的代码:

public class Second extends Activity {

TextView questionTextView;
DBAdapter db = new DBAdapter(this);
Cursor mCursor;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    TextView questionTextView = (TextView) findViewById(R.id.question);
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
    RadioButton rb0 = (RadioButton) findViewById(R.id.radio0);
    RadioButton rb1 = (RadioButton) findViewById(R.id.radio1);
    RadioButton rb2 = (RadioButton) findViewById(R.id.radio2);

    db.open();
    db.getQuestionSet(1);
    String question = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_QUESTIONS));
    String answer = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_ANSWERS));
    String opt1 = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_CHOICE1));
    String opt2 = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_CHOICE2));
    questionTextView.setText(question);
    rb0.setText(answer);
    rb1.setText(opt1);
    rb2.setText(opt2);

}

}

我尝试运行时遇到错误。我知道我的代码不正确。但我不知道如何纠正它。更具体地说,我要做的是创建一个应用测验。我设法创建了一个数据库。我可以通过应用程序插入/更新/删除问题和答案。但我无法显示存储在我的数据库中的问题,因此我可以回答问题并对其进行评分。谢谢你的帮助。我是初学者。一直在研究eclipse一个月..我甚至无法创建一个简单的测验应用程序。 :(

谢谢。这是Logcat中显示的错误:

 09-03 15:29:49.516: D/AndroidRuntime(4091): Shutting down VM
 09-03 15:29:49.516: W/dalvikvm(4091): threadid=1: thread exiting with uncaught exception (group=0x40015560)
 09-03 15:29:49.527: E/AndroidRuntime(4091): FATAL EXCEPTION: main
 09-03 15:29:49.527: E/AndroidRuntime(4091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dtan.quiz/com.dtan.quiz.Second}: java.lang.NullPointerException
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.os.Handler.dispatchMessage(Handler.java:99)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.os.Looper.loop(Looper.java:123)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread.main(ActivityThread.java:3683)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at java.lang.reflect.Method.invokeNative(Native Method)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at   java.lang.reflect.Method.invoke(Method.java:507)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at dalvik.system.NativeStart.main(Native Method)
 09-03 15:29:49.527: E/AndroidRuntime(4091): Caused by: java.lang.NullPointerException
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at com.dtan.quiz.Second.onCreate(Second.java:30)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    ... 11 more

1 个答案:

答案 0 :(得分:1)

public List<Question> getQuestionSet(int i) {
    List<Question> questionSet = new ArrayList<Question>();
    final Cursor c = db.rawQuery("SELECT * FROM quiztable ORDER BY RANDOM() LIMIT " + i, null);
    if (c != null) {
        try {
            while (c.moveToNext()) {
                Question q = new Question();
                q.setQuestion(c.getString(1));
                q.setAnswer(c.getString(2));
                q.setOption1(c.getString(3));
                q.setOption2(c.getString(4));
                questionSet.add(q);
            }
        } finally {
            c.close();
        }
    }
    return questionSet;
}
  

编辑:

您尚未初始化Cursor。但是当你从List方法返回getQuestionSet()时,它就没用了,而是使用它。

List<Question> questions = db.getQuestionSet(1);
if (questions != null && !questions.isEmpty()) {
   Question question = questions.get(0);
   now use:
   question.getQuestion();
   question.getAnswer();
   question.getOption1();
   question.getOption2();
}