android - 用于填充 ListView 的 SimpleCursorAdapter

标签 android simplecursoradapter

我正在使用一个数据库,我试图用我的表的每一行填充一个 ListView。我已成功创建和查询数据,但无法使用 SimpleCursorAdapter 在 ListView 中显示任何内容。

public class History extends ListActivity {
SQLiteDatabase db;
DbHelper DbHelper;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.history);
    DbHelper = new DbHelper(this);
    db = DbHelper.getWritableDatabase();
    lv = getListView();

    final String[] from = { DbHelper.TYPE, DbHelper.TITLE, DbHelper.CONTENT };
    final String[] column = { DbHelper.C_ID, DbHelper.TYPE, DbHelper.TITLE,
            DbHelper.CONTENT };

    final int[] to = { R.id.list_row_title, R.id.list_row_content };
    Cursor cursor = db.query(DbHelper.TABLE_NAME, column, null, null, null,
            null, null);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.history_list_row, cursor, from, to);

    lv.setAdapter(adapter);

    /*
     * Display as Toast for development purposes
     */
    // move to first row
    cursor.moveToFirst();
    // move to the next item each time
    while (cursor.moveToNext()) {
        // get string and column index from cursor
        String name = cursor
                .getString(cursor.getColumnIndex(DbHelper.TYPE));
        String title = cursor.getString(cursor
                .getColumnIndex(DbHelper.TITLE));
        String content = cursor.getString(cursor
                .getColumnIndex(DbHelper.CONTENT));

        Toast.makeText(this,
                "Title: " + title + "\n" + "Content: " + content,
                Toast.LENGTH_SHORT).show();
    }
    cursor.close();
}

这是我自定义的 ListView 行,用于显示数据库中的 3 个字符串

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:id="@+id/list_row_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Title"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/list_row_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/list_row_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Type"
    android:textAppearance="?android:attr/textAppearanceSmall" />

Toast 消息显示完美,但 ListView 中没有任何显示。

最佳答案

我注意到这里有几个问题。

首先,

final int[] to = { R.id.list_row_title, R.id.list_row_content };

应该是

final int[] to = { R.id.list_row_type, R.id.list_row_title, R.id.list_row_content };

其次,您设置的适配器不正确。您可以直接设置适配器,而不是获取 ListView 和设置适配器:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
        R.layout.history_list_row, cursor, from, to);
setListAdapter(adapter);

第三,您将在循环后关闭游标。

//Closes the Cursor, releasing all of its resources and making it completely invalid.
cursor.close();

所以当 ListView 显示项目时,游标不再有与之关联的行

关于android - 用于填充 ListView 的 SimpleCursorAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15014926/

相关文章:

android - CardView 内的圆角底部 TextView

android - 带有 CursorAdapter 的 ListView 不显示我的值

java - Android:Java:TextView 不会改变颜色

android - SimpleCursorAdapter 返回字符串的整数键 - 如何转换为相应的字符串?

android - 根据光标结果更改 ListView 项的背景颜色

android - ListView 性能低下

android - CSS 触摸 Action : not always disabling in Cordova app

android - SQL 按列排序,0 为异常(exception)

android - 在 Android 上向 AES-GCM 添加额外的经过身份验证的数据

android - 如何在 android api < 21 上添加海拔