1

Function For Selecting data in Database helper:

 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;
}

I have called this function in main activity like this but it only return first value of the table but i want it to return whole values of table so I can save it to the list!:

   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);
            }


        }
    });

Here is a function for setting question view!

   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++;

} 
Raheel Rehman
  • 85
  • 1
  • 7

1 Answers1

1

Note these lines:

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

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

You moved the cursor twice. just add the null filter with your moveToFirst()

if (cursor != null && cursor.moveToFirst()) {
 // stuffs
}
Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22