Shruti
Shruti

Reputation: 5591

Android Sqlite Cursur not returning correct values

I have used cursor on sqlite db for getting records .

My code is following:

btnnext.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        displayQuestion();
    });
}   

private void displayQuestion() {
    db.openDataBase();
    SQLiteDatabase sqlitedatabase = db.getWritableDatabase();

    Cursor cur;
    cur = sqlitedatabase.rawQuery("select quiz_id, opt_ans from options" ,null);
    cur.moveToFirst();
    db.close();

    radioGroup.removeAllViews();
    for (int i = 0; i <= 3; i++) {
        l++;
        Log.v("value of l",l+"");
        radioButton = new RadioButton(this);
        String str_opt = cur.getString(cur.getColumnIndex(Database_helper_class.OPT_ID));
        radioButton.setText(str_opt);
        Log.v("value of l",l+""+str_opt);
        radioButton.setId(i);
        cur.moveToPosition(l);
        radioGroup.addView(radioButton);
    }
}

and the log I am getting is :

03-07 18:32:03.190: V/value of l(28903): 1video
03-07 18:32:03.200: V/value of l(28903): 2
03-07 18:32:03.210: V/value of l(28903): 2sound
03-07 18:32:03.210: V/value of l(28903): 3
03-07 18:32:03.221: V/value of l(28903): 3image
03-07 18:32:03.231: V/value of l(28903): 4
03-07 18:32:03.240: V/value of l(28903): 4word document
03-07 18:32:05.561: V/value of l(28903): 5
03-07 18:32:05.571: V/value of l(28903): 5video
03-07 18:32:05.580: V/value of l(28903): 6
03-07 18:32:05.590: V/value of l(28903): 6A process
03-07 18:32:05.590: V/value of l(28903): 7
03-07 18:32:05.601: V/value of l(28903): 7A system software
03-07 18:32:05.601: V/value of l(28903): 8
03-07 18:32:05.610: V/value of l(28903): 8A document

and the db which I am using is

opt_ans,"opt_id","quiz_id"
    video,"1","1"
    sound,"2","2"
    image,"3","3"
    word document,"4","4"
    appliction,"5","5"
    A process,"6","6"
    A system software,"7","7"
    A document,"8","8"
    hgfhgfh,"9","9"
    hgfhgf,"10","10"
    tytrytr,"11","11"
    ytrytrytr,"12","12"
    tytrytrytr,"13","13"
    tytrytryt,"14","14"
    tytyt,"15","15"
    ytytyt,"16","16"
    ytytryt,"17","17"
    tytytr,"18","18"
    tytytryt,"19","19"
    htghyt,"20","20"

Now my question:

Why am I getting video on 5th and 9th index also while having different values at those indexes ??

Upvotes: 0

Views: 184

Answers (1)

Caner
Caner

Reputation: 59168

SQL does not guarantee any order of the returned rows, unless you specify. Basicly your results could be in random order. So you need to change your query as follows:

cur = sqlitedatabase.rawQuery("select quiz_id, opt_ans from options order by quiz_id asc" ,null);

EDIT
Also I guess you are not iterating over the cursor properly, try this:

int i = 0;
for (boolean hasItem = cur.moveToFirst(); hasItem; hasItem = cur.moveToNext()) {
    if (i < 4) {
        radioButton = new RadioButton(this);
        String str_opt = cur.getString(cur.getColumnIndex(Database_helper_class.OPT_ID));
        radioButton.setText(str_opt);
        radioButton.setId(i);
        radioGroup.addView(radioButton);
        Log.v("Value of button " + i + " is: " + str_opt);
        i++;
    }
    else {
        break;
    }
}

Upvotes: 1

Related Questions