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

发布于 2024-12-20 08:14:56 字数 1811 浏览 3 评论 0原文

我想检索用户的分数。用户可以有多个分数。但如果用户有多个分数,则仅显示最后一个分数,而不是所有分数。

这些是我的代码

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨小墨 2024-12-27 08:14:56

每次在循环中都覆盖数据和对象
试试这个

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 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());
离去的眼神 2024-12-27 08:14:56

每次光标迭代都会覆盖文本视图中的值。您需要将光标结果放入数组中,然后将它们连接成一个长字符串,或者使用可以处理多个值的小部件,例如:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文