2013-02-22 145 views
1

我正在处理数据库,我试图用我的表的每一行填充一个ListView。我已经成功创建并查询了数据,但我无法使用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行从数据库

<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" /> 

敬酒消息显示完美显示3个字符串,但没有出现在ListView。

+3

我不认为你应该搞砸,更不要关闭适配器外的光标,因为它会弄乱光标位置和加载数据。关闭的光标在你的适配器内不好。 – ebarrenechea 2013-02-22 00:45:50

+0

如果你不'',你可以将'null'传递给['changeCursor(Cursor)'](http://developer.android.com/reference/android/widget/CursorAdapter.html#changeCursor(android.database.Cursor)不再需要它了。如果你使用['LoaderCallbacks'](http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html),你可以在['onLoaderReset()'](http:// developer .android.com/reference/android/app/LoaderManager.LoaderCallbacks.html#onLoaderReset(android.content.Loader ))。否则,请勿关闭适配器外部的光标。适配器管理光标本身。 – 2013-02-22 01:05:00

回答

1

我在这里注意到一些问题。

首先,

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

因此,通过列表视图中显示的项目时,光标不再具有与其

+0

感谢您的帮助,这解决了我的问题!但我有个问题。我正在按照指示在光标上调用close()的教程,否则它将无法工作。采取这条线解决了我的问题,所以我的问题是什么时候适当关闭光标? – ericcarboni 2013-02-24 16:35:00

0

您将需要设置的TextView在while循环文本相关的行。

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