我在使用自定义CursorWrapper从我的SQLitedatabase获取" question"对象时遇到问题。我使用一个查询函数返回CursorWrapper,其中所有行都有问题,这些行具有给定的章节索引,并且学习的整数是0.必须存在我在查询函数中找不到的语法错误,因为QuestionCursor来自于此函数有0行,数据库中有三个符合条件的问题。如果你能发现错误会很棒。
我的SQLiteAssetHelper中的查询函数,它返回一个有0行的QuestionsCursor:
public QuestionCursor queryUnlearnedQuestions(int chapterId) {
Cursor wrapped = getReadableDatabase().query
(TABLE_QUESTIONS,
null,
COLUMN_QUESTIONS_CHAPTER_ID + " = ? AND " + COLUMN_QUESTIONS_LEARNED + " = ? ",
new String[] {String.valueOf(chapterId), String.valueOf(0)},
null,
null,
COLUMN_QUESTIONS_QUESTION_ID + " asc");
return new QuestionCursor(wrapped);
}
自定义CursorWrapper
public static class QuestionCursor extends CursorWrapper {
public QuestionCursor(Cursor cursor) {
super(cursor);
// TODO Auto-generated constructor stub
}
public Question getQuestion(){
if(isBeforeFirst()||isAfterLast()) return null;
Question question = new Question();
question.setQuestionId(getInt(getColumnIndex(COLUMN_QUESTIONS_QUESTION_ID)));
question.setCategoryId(getInt(getColumnIndex(COLUMN_QUESTIONS_CHAPTER_ID)));
question.setDescription(getString(getColumnIndex(COLUMN_QUESTIONS_DESCRIPTION)));
question.setQuestionText(getString(getColumnIndex(COLUMN_QUESTIONS_QUESTION_TEXT)));
question.setAnswerText(getString(getColumnIndex(COLUMN_QUESTIONS_ANSWER_TEXT)));
question.setIsLearned((getInt(getColumnIndex(COLUMN_QUESTIONS_LEARNED)) == 1) ? true:false);
question.setCorrectAnswers(getInt(getColumnIndex(COLUMN_QUESTIONS_CORRECT_ANSWERS)));
question.setFalseAnswers(getInt(getColumnIndex(COLUMN_QUESTIONS_FALSE_ANSWERS)));
question.setLastAnswerDate(new Date(getLong(getColumnIndex(COLUMN_QUESTIONS_LAST_ANSWER_DATE))));
return question;
}
}
Singleton调用该函数并将对象放入具有固定长度的ArrayList中。
public ArrayList<Question> getUnlearnedQuestions(int chapterId, int maxlength) {
ArrayList<Question> unlearnedQuestions = new ArrayList<Question>();
QuestionCursor cursor = mHelper.queryUnlearnedQuestions(chapterId);
Log.d(TAG, ""+cursor.getCount()); // returns 0
for(cursor.moveToFirst(); cursor.getPosition() < maxlength; cursor.moveToNext()){
if(!cursor.isAfterLast()){
unlearnedQuestions.add(cursor.getQuestion());
}else{
break;
}
}
cursor.close();
return unlearnedQuestions;
}
整个LearnMode活动,尚未完成。
public class LearnModeActivity extends FragmentActivity {
public static String EXTRA_CHAPTER_ID = "LearnFragment.chapter_id";
private static String TAG = "LearnModeActivity";
private static int LEARN_ARRAY_LENGTH = 7;
private int mChapterId;
private ArrayList<Question> mQuestionList;
private ViewPager mViewPager;
protected void onCreate(Bundle args) {
// TODO Auto-generated method stub
super.onCreate(args);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.view_pager);
setContentView(mViewPager);
mQuestionList = ChapterManager.get(this).getUnlearnedQuestions(mChapterId, LEARN_ARRAY_LENGTH);
Log.d(TAG, "Question List size: "+ mQuestionList.size()); //returns 0
FragmentManager fm = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
@Override
public int getCount() {
//should return mQuestionList.size(); Changed it to three, otherwise getCount is 0 and I get a blank screen and there is no IndexOutOfBondException
return 3;
}
@Override
public Fragment getItem(int pos) {
//Indexoutofboundsexception right here
Question question = mQuestionList.get(pos);
return LearnModeFragment.newInstance(question.getQuestionId());
}
});
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int index) {
Question question = mQuestionList.get(index);
setTitle(question.getQuestionId()+". "+question.getDescription());
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
}
提前致谢