【问题标题】:retrieve data from database which contain more than one data从包含多个数据的数据库中检索数据
【发布时间】:2011-12-07 23:43:53
【问题描述】:

我想从 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"/>

我希望有人可以提供帮助。谢谢

【问题讨论】:

    标签: android database sqlite cursor


    【解决方案1】:
    cursor.getCount() == 1
    

    意味着只有当游标正好有一行时才执行该代码。

    无论如何,如果您还希望能够显示多个结果,您应该重新考虑您的布局(也许列表视图会更合适)。

    【讨论】:

    • 谢谢@fedepaol。你有什么推荐的教程可以作为我的布局参考吗?
    • 只需寻找 listview / listactivity 和 simplecursoraapter。如果您不需要元素的复杂布局,那应该绰绰有余。
    猜你喜欢
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 2015-09-10
    • 2013-03-02
    • 1970-01-01
    相关资源
    最近更新 更多