简体   繁体   English

android sqlite游标:获取多个类似数据

[英]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 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 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 谢谢

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

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM