2016-04-25 38 views
1
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和代碼錯誤:無法從cursorWindow讀取行0,col -1確保遊標在從其訪問數據之前正確初始化?

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中)。

+0

可能是你的查詢字符串有問題。請確保你的表'問題'和列'subject_id'拼寫正確..... – Opiatefuchs

+0

這將是有益的,看到你的代碼,你正在初始化數據庫和表(包括SQL字符串請......).... – Opiatefuchs

回答

0

問題是您的光標不包含任何結果。可能是由錯誤的查詢造成的。

相關問題