2014-10-02 29 views
0

我不是很熟悉android編程,但我仍然可以理解代碼,我唯一的問題是我無法獲得我在android eclipse上的遊戲應用所需的輸出,我只是想從數據庫中檢索數據並顯示從3個不同的文本字段的高分例如:Android SQLite:如何使用Order By和Descending函數使用rawquery?

TextField1 <-- this will be the highest score of a player will be displayed 
TextField2 <-- this will be the second highest next to textfield1 
TextField3 <-- this will be the third highest score 

我真的很倒黴的編碼我認爲這將是很容易像PHP做的事情,但吮吸我的即時通訊android的新手,如果有人能幫助我請做:)

順便說一句:這是代碼,我一直在工作,但我沒有任何運氣來顯示我想要的

try{ 
db = openOrCreateDatabase("DodgerDB",MODE_PRIVATE,null); 

Cursor c = db.rawQuery("select * from ScoresTable order by scoreVALUE desc",null); 

while(c.moveToNext()){ 
scoreid = c.getInt(c.getColumnIndex("scoreID")); 

score = c.getInt(c.getColumnIndex("scoreVALUE")); 

date = c.getString(c.getColumnIndex("scoreDATE")); 


ScoreView1.setText("score id:"+scoreid+" score:"+score+" date:"+date); 

ScoreView2.setText("score id:"+scoreid+" score:"+score+" date:"+date); 

ScoreView3.setText("score id:"+scoreid+" score:"+score+" date:"+date); 

}//end of while loop }//end of try statement 

catch(Exception er) { 
Toast.makeText(ScoresActivity.this, ""+er, Toast.LENGTH_SHORT).show(); 
} 

還是我只拿到了相同的張玉峯(DATAS)輸出數據庫來對這些3場,請幫助:(

回答

0

在這種情況下,你會看到你的光標只是最後一排。每次迭代都從Cursor中檢索一行,並且每次都會覆蓋TextView中的以前值。

試試這個理解它是如何工作的:

while(c.moveToNext()){ 
scoreid = c.getInt(c.getColumnIndex("scoreID")); 

score = c.getInt(c.getColumnIndex("scoreVALUE")); 

date = c.getString(c.getColumnIndex("scoreDATE")); 

Log.d("SomeTag", "score id:"+scoreid+" score:"+score+" date:"+date); 

} 

UPD:要顯示在不同TextViews的3行:

c.moveToNext(); 
scoreid = c.getInt(c.getColumnIndex("scoreID")); 
score = c.getInt(c.getColumnIndex("scoreVALUE")); 
date = c.getString(c.getColumnIndex("scoreDATE")); 
ScoreView1.setText("score id:"+scoreid+" score:"+score+" date:"+date); 

c.moveToNext(); 
scoreid = c.getInt(c.getColumnIndex("scoreID")); 
score = c.getInt(c.getColumnIndex("scoreVALUE")); 
date = c.getString(c.getColumnIndex("scoreDATE")); 
ScoreView2.setText("score id:"+scoreid+" score:"+score+" date:"+date); 

c.moveToNext(); 
scoreid = c.getInt(c.getColumnIndex("scoreID")); 
score = c.getInt(c.getColumnIndex("scoreVALUE")); 
date = c.getString(c.getColumnIndex("scoreDATE")); 
ScoreView3.setText("score id:"+scoreid+" score:"+score+" date:"+date); 
+0

是啊,我只得到了最後的數據,但它的最大價值,所以我需要在這2個文本框中顯示2個更多的數據,我已經沒有想法了,能否幫我解決這個問題? – iMbaX 2014-10-02 09:59:45

+0

增加了更新。你也必須添加檢查,如果光標不爲空,計數不小於3 – balytskyi 2014-10-02 14:19:47

+0

哇,你真的幫助我,謝謝你的時間,我很感激它,如果它有效,我會讓你知道:)謝謝 – iMbaX 2014-10-02 18:44:29