SimpleCursorAdapter to populate ListView

我只是一个虾纸丫 提交于 2019-12-11 10:33:02

问题


I'm working with a database and I'm trying to populate a ListView with each row of my table. I've successfully created and queried data, but I can't get anything to appear in the ListView using a SimpleCursorAdapter.

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

}

This is my custom ListView row to display 3 Strings from the database

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

The Toast messages appear perfectly, but nothing appears in the ListView.


回答1:


I'm noticing a couple of issues here.

First,

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

should be

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

Secondly, you are setting the adapter incorrectly. Instead of getting the list view and setting the adapter, you can set the adapter directly:

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

Third, you are closing the cursor after the loop.

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

So by the time the list view is displaying items, the cursor no longer has the rows associated with it




回答2:


You will need to set the TextView's text in the while loop.

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


来源:https://stackoverflow.com/questions/15014926/simplecursoradapter-to-populate-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!