ANDROID Sqli游标只返回1行,然后它给出Null Exception

时间:2015-12-22 14:17:57

标签: java android sqlite oop arraylist

功能用于在数据库帮助程序中选择数据:

 public List<Question> slectQuestionsC(String tab_name) {

    List<Question> quesList = new ArrayList<Question>();
    // Select All Query
    SQLiteDatabase db = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM " + tab_name ;

    try {
    Cursor cursor = db.rawQuery(selectQuery, null);

    if(cursor != null){
        cursor.moveToFirst();
    }

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        if (cursor.getCount() > 0) {
        do {
            Question Quiz = new Question();
            Quiz.setID(cursor.getInt(0));
            Quiz.setQUESTION(cursor.getString(1));
            Quiz.setOPT1(cursor.getString(2));
            Quiz.setOPT2(cursor.getString(3));
            Quiz.setOPT3(cursor.getString(4));
            Quiz.setOPT4(cursor.getString(5));
            Quiz.setANSWER(cursor.getString(6));
            Quiz.setHINT(cursor.getString(7));
            quesList.add(Quiz);
        } while (cursor.moveToNext());
    }
    }
    } catch (Exception e) { e.printStackTrace(); }

    return quesList;
}

我在这样的主要活动中调用了这个函数,但是它只返回表的第一个值,但是我希望它返回表的整个值,所以我可以将它保存到列表中!: < / p>

   Question currentQ;
   List<Question> quesList;
   int qid = 0;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    db.Open();


        quesList = db.slectQuestionsC(tab_name);
        currentQ = quesList.get(qid);

        setQuestionView();

   next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int ansID = options.getCheckedRadioButtonId();
            RadioButton rb_ans = (RadioButton)findViewById(ansID);
            String answer = rb_ans.getText().toString();
            Log.d("yourans", currentQ.getANSWER()+" "+answer);


            if(currentQ.getANSWER().equals(answer))
            {
                scoreT++;
                Log.d("score", "Your score"+scoreT);
            }

            if (qid < 6)
            {
                try {
                currentQ=quesList.get(qid);
                setQuestionView();
                } catch (Throwable e) {
                    e.printStackTrace();
                    Toast("Exception" + e.toString());
                }

            }
            else 
            {
                qid = 1;
                Intent intent = new Intent(CustomQuizActivity.this, Quiz_End.class);
                Bundle b = new Bundle();
                b.putInt("score", scoreT); //Your score
                intent.putExtras(b); //Put your score to your next Intent
                startActivity(intent);
            }


        }
    });

以下是设置问题视图的功能!

   private void setQuestionView()
   {

    question_no.setText("Question #" + qid);
    question_text.setText(currentQ.getQUESTION());
    op1.setText(currentQ.getOPT1());
    op2.setText(currentQ.getOPT2());
    op3.setText(currentQ.getOPT3());
    op4.setText(currentQ.getOPT4());



    qid++;

} 

1 个答案:

答案 0 :(得分:0)

请注意以下几行:

if(cursor != null){
   cursor.moveToFirst();
}

// looping through all rows and adding to list
if (cursor.moveToFirst()) {

您将光标移动了两次。只需使用null

添加moveToFirst()过滤条件即可
if (cursor != null && cursor.moveToFirst()) {
 // stuffs
}