从数据库中检索包含多个数据的数据

时间:2011-12-07 23:43:53

标签: android database sqlite cursor

我想从sqlite中检索数据。用户可以有超过1分,我想显示它。它可以显示用户是否只有一个分数,但如果用户有多个分数,则显示空白屏幕。

这些是我的代码

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.getCount() == 1)
    {
            cursor.moveToFirst();

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


    }

}}

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 个答案:

答案 0 :(得分:2)

cursor.getCount() == 1

表示只有在游标只有一行时才执行该代码。

在任何情况下,如果您还希望能够显示多个结果,则应重新考虑您的布局(也许列表视图更合适)。

相关问题