Android SimpleCursorAdapter + ListView 不工作

标签 android sqlite listview cursor simplecursoradapter

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

我的代码如下:

在 Activity 中:由 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" />

最佳答案

我想出了问题。当方法结束时光标在那之后变得无效时,我犯了一个关闭连接的愚蠢错误。

关于Android SimpleCursorAdapter + ListView 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5891417/

相关文章:

android - 删除 ImageView 的阴影

android - Flutter FFmpeg - [AVFilterGraph @ 0x88f4b300] 没有这样的过滤器 : '"scale'

ios - 为什么要创建一个空数据库(0 口)?

c# - 在 SQLite.Net 中存储 DateTimeOffset

c++ - 从 PNG 加载 Gdiplus 的 HICON 质量低

android - 在 Kotlin 中的 Fragment 类之间切换

android - 线程是否停止执行并继续?这只是令人困惑

sqlite - 导入前如何验证数据库中不存在记录?

java - 带有比较器的Android排序 ListView 不起作用

android - 如何知道ListView的最后一行是否显示完整?