【发布时间】:2015-05-26 10:51:56
【问题描述】:
我正在尝试进行多项答案测验。我关注了这个网站: Simple Quiz Game
在Multiple Activity中,我添加了这个:
QuestionsDbHelper db = new QuestionsDbHelper(this);
questionsList = db.getAllQuestions();
currentQuestion=questionsList.get(questionId);
正如您在最后一行看到的那样,我收到了这个错误。在 QuestionsDbHelper 中,我添加了问题,但仍然无法将它们显示在 Activity 上。 我已经在 Manifest 文件中描述了 Activity。 从 Debug 中我可以理解的是我的 Array 是空的,但是我已经插入了问题列表。
有什么想法吗?
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at com.game.mlm.q2game.MultipleMode.onCreate(MultipleMode.java:36)
我发布的代码没有 onCreate 和 onUpgrade,因为它们非常基础。 这是我的 QuestionsDbhelper 代码:
public void addQuestions(Questions question){
Log.d("add Questions" , question.toString());
// SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUESTION, question.getQuestion());
values.put(KEY_ANSWER, question.getAnswer());
values.put(KEY_FBUTTON, question.getFirstButton());
values.put(KEY_SBUTTON, question.getSecondButton());
values.put(KEY_THBUTTON, question.getThirdButton());
dbase.insert(TABLE_QUESTIONS, null, values);
}
public List<Questions> getAllQuestions(){
List<Questions> questionsList = new ArrayList<Questions>();
//SELECT ALL FROM QUERY
String selectQuery = "SELECT * FROM " + TABLE_QUESTIONS;
dbase= this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
//LOOPING THROUGH ALL ROWS AND ADDING TO LIST
if (cursor.moveToFirst()) {
do {
Questions question = new Questions();
question.setId(cursor.getInt(0));
question.setQuestion(cursor.getString(1));
question.setAnswer(cursor.getString(2));
question.setFirstButton(cursor.getString(3));
question.setSecondButton(cursor.getString(4));
question.setThirdButton(cursor.getString(5));
questionsList.add(question);
}
while (cursor.moveToNext());
}
Log.d( "getAllQuestions()", questionsList.toString() );
return questionsList;
}
public int rowcount() {
int row = 0;
String selectQuery = "SELECT * FROM " + TABLE_QUESTIONS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row = cursor.getCount();
// cursor.close();
return row;
}
【问题讨论】:
-
Any idea?ArrayList 为空(可能查询返回空结果集)。 -
你能把
QuestionsDbHelper的代码加进去吗 -
在 Logcat 中打印查询并在 SQLite 浏览器上运行。可能是您的查询错误。
-
EpicPandaForce,我已经添加了我的 QuestionsDbHelper,但列表仍然是 0