Android SimpleCursorAdapter + ListView not working
Hi I ma trying to use a SimpleCursorAdapter to bind values to a list adapter. The cursor is comming from the SQLLite database. However regardless of the results in the cursor (It always has atleast one row) the list view displays the empty message.I am pretty new to android and appreciate any help on this.
My code is given below:
In Activity : Called by 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);
}
Retrieving the cursor
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
<Line开发者_JS百科arLayout 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>
Main Layout
<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" />
I Figured out the problem. I was doing the dumb mistake of closing down the connection when the method ends where cursor gets invalid after that.
精彩评论