2011-12-08 54 views
0

我想檢索用戶的分數。用戶可以有多個分數。但是如果用戶有不止一個分數,它只顯示最後的分數,而不是所有的分數。android sqlite光標:獲取多個類似的數據

這是我的代碼

ScoreDetailActivity.java

public class ScoreDetailActivity extends Activity { 

    protected TextView userName; 
    protected TextView score; 
    protected int user_Id; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.scoredetail); 

    user_Id = getIntent().getIntExtra("USER_ID", 0); 
    SQLiteDatabase db = (new DatabaseUsername(this)).getWritableDatabase(); 
    Cursor cursor = db.rawQuery("SELECT alm._id, alm.nama, alm.jekel, sc._id, sc.score, sc.userId FROM almag alm LEFT OUTER JOIN score sc ON alm._id = sc.userId WHERE alm._id = ?", 
          new String[]{""+user_Id}); 

      if (cursor.moveToFirst()){ 
       do { 
         userName = (TextView) findViewById(R.id.userName); 
         userName.setText(cursor.getString(cursor.getColumnIndex("nama"))); 
          score = (TextView) findViewById(R.id.score); 
          score.setText(cursor.getString(cursor.getColumnIndex("score"))); 
        } while (cursor.moveToNext()); 
       } 

}} 

scoredetail.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
    <TextView 
      android:id="@+id/userName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    <TextView 
      android:id="@+id/score" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

我希望有人能幫助我。謝謝

回答

1

你重寫你的數據,並在循環 對象的時候試試這個

userName = (TextView) findViewById(R.id.userName); 
userName.setText(cursor.getString(cursor.getColumnIndex("nama"))); 
score = (TextView) findViewById(R.id.score); 

StringBuffer data = new StringBuffer(); 
do { 
    data.append(cursor.getString(cursor.getColumnIndex("score"))+"\n"); 
}while(cursor.moveToNext()); 
score.setText(data.toString()); 
+0

謝謝@Pratik。是工作 :) – kumisku

0

你改寫值在文本視圖通過光標每次迭代。您需要將遊標結果放入數組中,然後將它們連接成一個長字符串,或使用可處理多個值的小部件,如:

<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textMultiLine"> 
</EditText>