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"/>
我希望有人可以帮助我。谢谢
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次在循环中都覆盖数据和对象
试试这个
you override your data and object every time in the loop
try this
每次光标迭代都会覆盖文本视图中的值。您需要将光标结果放入数组中,然后将它们连接成一个长字符串,或者使用可以处理多个值的小部件,例如:
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: