Impossible to close Cursor when setting to SimpleCursorAdapter
Im p开发者_高级运维erforming certain database operations after which I attach Cursor to the SimpleCursorAdapter for display List on my ListActivity. If I close the cursor in finally block (or anywhere after the code below), nothing is displayed in the list
SimpleCursorAdapter scAdapter = new SimpleCursorAdapter(this,
R.layout.list_row, cursor,
new String[] { Constants.KEY_TITLE }, new int[] { R.id.text1 });
If the cursor is not closed, Android throws exception when activity makes request to another activity or is passivated.
PS I tried closing cursor inside onPause(), but android complains before onPause is called.
You have to leave open the cursor on your finally block.
You missed out to call in your activity.
startManagingCursor(cursor);
This will make sure the activity manages the Cursor for you.
I get around this by creating the cursor on the fly. It appears that the SimpleCursorAdapter will manage the cursor for you.
SimpleCursorAdapter scAdapter = new SimpleCursorAdapter(this,
R.layout.list_row, db.query(...),
new String[] { Constants.KEY_TITLE }, new int[] { R.id.text1 });
Also since Pentium10's post, startManagingCursor has been deprecated.
That only solves part of the problem. When I startManagingCursor(cursor); and hit Menu key > menuActivity then > back key I get:
ERROR/Cursor(4047): Invalid statement in fillWindow()
精彩评论