【问题标题】:Getting questions from SQLite Android从 SQLite Android 获取问题
【发布时间】: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

标签: android sqlite arraylist


【解决方案1】:

您是否检查过 questionId 是否正确?您可能没有您使用的 questionId 的数据

【讨论】:

  • 将此作为评论发布,而不是作为答案。
  • 我不被允许发表评论
【解决方案2】:

首先检查从您的数据库返回的 rowCount。 如果是,则使用

将光标移动到第一个位置
 cursor.moveToFirst();

【讨论】:

  • 我有你可以看到的光标,当我调试时我可以看到 questionList size =0;
【解决方案3】:

在QuestionsDbHelper class 的getAllQuestions() 方法中使用这些行。

String Query = "select * from <table name>";
db = openDataBase();
Cursor cursor = db.rawQuery(Query, null);
// Log.d(LOG_TAG, "Cursor Length: " + cursor.getCount());
while (cursor.moveToNext()) {
<Get all data from table>
}

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多