错误:无法从cursorWindow读取第0行,col -1,确保在从中访问数据之前正确初始化了游标?

时间:2016-04-25 12:34:08

标签: android

private void populateQuestionList() {
    int QuestionTableSubjectId = questionModelObject.getSubjectId();
    ArrayList<QuestionModel> questionArrayList = QuestionManager.getQuestionManager(this).getQuestionBySubjectID(QuestionTableSubjectId);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < questionArrayList.size(); i++) {
        TableRow row = (TableRow) inflater.inflate(R.layout.item_ctrl_list_row, null);
        row.setOnClickListener(this);
        TextView questionTV = (TextView) row.findViewById(R.id.tv_ctrl_name);
        String question = questionArrayList.get(i).getQuestion();
        questionTV.setText(question);
        row.setTag(questionArrayList.get(i));
        questionList.addView(row);
    }
}

此方法用于从数据库和代码

列表调用方法getQuestionBySubjectID中显示问题
 public ArrayList<QuestionModel> getQuestionBySubjectID(int subjectId) {
    ArrayList<QuestionModel> questionArrayList = new ArrayList();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("select * from Questions where subject_id=" + subjectId, null);
    res.moveToFirst();
    while (res.isAfterLast() == false) {
        QuestionModel questionModelObject = new QuestionModel();

        questionModelObject.setQuestionId(res.getInt(res.getColumnIndex("questionId")));
        questionModelObject.setQuestion(res.getString(res.getColumnIndex("question")));
        questionModelObject.setQuestion(res.getString(res.getColumnIndex("answerOne")));
        questionModelObject.setQuestion(res.getString(res.getColumnIndex("answerTwo")));
        questionModelObject.setQuestion(res.getString(res.getColumnIndex("answerThree")));
        questionModelObject.setQuestion(res.getString(res.getColumnIndex("answerFour")));
        questionModelObject.setAnswerOne(res.getString(res.getColumnIndex("correctAnswer")));
        questionArrayList.add(questionModelObject);
        res.moveToNext();
    }


    return questionArrayList;
}

这段代码有什么问题?我不知道错误是什么(在Cursor中)。

1 个答案:

答案 0 :(得分:0)

问题是您的光标不包含任何结果。可能是由错误的查询引起的。