SimpleCursorAdapter用于填充ListView

时间:2013-02-22 00:37:35

标签: 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中没有任何内容。

2 个答案:

答案 0 :(得分:2)

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

首先,

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 };

其次,您正在错误地设置适配器。您可以直接设置适配器,而不是获取列表视图和设置适配器:

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

因此,当列表视图显示项目时,游标不再具有与之关联的行

答案 1 :(得分:0)

您需要在while循环中设置TextView的文本。

 TextView tv1=(TextView)findViewById(R.id.list_row_content);
 tv1.setText(content);