2011-12-08 16 views
0

私はユーザーのスコアを取得したいと思います。ユーザーは複数のスコアを持つことができます。しかし、ユーザーが複数のスコアを持っている場合は、すべてのスコアではなく最後のスコアのみを表示します。android sqlite cursor:複数の類似データを取得する

これらは私が誰かが私を助けることを願って、私のコード

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> 
関連する問題