kumisku
kumisku

Reputation: 103

android sqlite cursor: fetch more than one similar data

I want to retrieve the scores of the user. User can have more than one score. But if user has more than one score, it just show the last score, not all of the scores.

These are my codes

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"/>

I hope someone can help me. Thank you

Upvotes: 0

Views: 1129

Answers (2)

TomDestry
TomDestry

Reputation: 3399

You're overwriting the values in your text views with each iteration through the cursor. You need to put the cursor results into an array and then either concatenate them into a long string, or use a widget that can handle multiple values like:

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

Upvotes: 0

Pratik
Pratik

Reputation: 30855

you override your data and object every time in the loop try this

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

Upvotes: 1

Related Questions