1

您好我正在尝试使用 SimpleCursorAdapter 将值绑定到列表适配器。游标来自 SQLLite 数据库。但是,无论光标中的结果如何(它总是至少有一行),列表视图都会显示空消息。我对 android 很陌生,感谢任何帮助。

我的代码如下:

在活动中:由 onCreate() 调用

private void setupList() {
    if(sesionDao == null) {
        sesionDao = new SessionDAO(this);
    }
    Cursor cursor = sesionDao.retrieveAllSessions();
    startManagingCursor(cursor);

    String[] from = new String[]{Database.SES_SESSION_NAME};
    int[] to = new int[]{R.id.sessionNameRow};
    SimpleCursorAdapter sessionAdapter = new SimpleCursorAdapter(this, R.layout.session_list_row,cursor, from, to );

    setListAdapter(sessionAdapter);
}

检索光标

db = dbHelper.getReadableDatabase();

Cursor c = db.query(Database.SESSIONS_TABLE_NAME, new String[] {
    Database.ID_COLUMN, Database.SES_SESSION_NAME }, null,
                null, null, null,null);

session_list_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="" android:id="@+id/sessionNameRow" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </LinearLayout>

主要布局 <ListView android:id="@android:id/list" android:layout_height="fill_parent" android:layout_width="fill_parent"></ListView> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_sessions_msg" />

4

1 回答 1

2

我解决了这个问题。当方法结束时光标变得无效时,我犯了关闭连接的愚蠢错误。

于 2011-05-05T16:57:03.153 回答