Reputation: 473
Is this method correct?
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;
If yes, how do i create a method of displaying each question from my database individually in random order.. sorry. i have no idea what to do...
EDIT: I tried changing the code above to your code. Now another question. This is the code i have in my MainActivity file:
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);
}
}
I get an error when i try to run it. i know my code is incorrect. but i am not sure how to correct it. To be more specific, what i am trying to do is create an app quiz. I managed to create a database. I could insert/update/delete questions and answers thru the application. But i can't display the questions stored in my database so i could answer the questions and grade it. thank you for your help guys. Im a beginner. been studying eclipse for a month now.. and i cant even create a simple quiz app. :(
Thank you. Here is the error shown in 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
Upvotes: 0
Views: 425
Reputation: 13247
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;
}
EDIT:
You have not initialized your Cursor
. But it's useless as you are returning List
from your getQuestionSet()
method, use it instead.
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();
}
Upvotes: 1