这种方法正确吗?
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