2012-03-07 31 views
0

我已經使用光標在sqlite數據庫獲取記錄。Android的Sqlite Cursur沒有返回正確的值

我的代碼如下:

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

和我得到的日誌是:

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 

和我使用的數據庫是

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" 

現在我的問題:

爲什麼我會得到v意識到5日和9日的指數也在這些指數有不同的價值?

+0

將是巨大的,如果你可以清理註冊你的代碼。刪除所有註釋行,因爲它們對我們來說不是很有趣。這將使我們更容易以這種方式閱讀您的代碼。 – WarrenFaith 2012-03-07 13:20:04

回答

1

SQL不保證返回行的任何順序,除非您指定。基本上你的結果可以是隨機的。所以,你需要改變你的查詢,如下所示:

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

編輯
此外,我猜你是不是遍歷光標正常,試試這個:

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

我已經嘗試過,但沒有幫助 – Shruti 2012-03-07 13:49:53

+0

請參閱我的編輯 – Caner 2012-03-07 13:58:39

+0

這並不能解釋爲什麼「視頻」是重複的。 – RivieraKid 2012-03-07 14:02:44