Calling startManagingCursor twice makes my data go away
I have this method which I use to fill a listview with data from a sqlite database. It works as expected when the application is started:
private void populateListView() {
Cursor showsCursor = mDbHelper.fetchAllSummaries();
startManagingCursor(showsCursor);
// Create an array to specify the fields we want to display in the list
String[] from = new String[]{DbAdapter.SUMMARY_TITLE, DbAdapter.SUMMARY_DATE, DbAdapter.SUMMARY_SUMMARY};
// and an array of the fields we want to bind those fields to
int[] to = new int[]{R.id.showtitle, R.id.showdate, R.id.showsummary};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter shows =
new SimpleCursorAdapter(this, R.layout.custom_row_view, showsCursor, from, to);
setListAdapter(shows);
}
Then I also have a Refresh menu item that updates the database and calls populateListView() again to fill the listview with the updated data. That also appears to work ok.
But no exactly! Because if I start another activity (eg. by clicking on one of the list view items) and then go back using the back button, my listview is empty. Calling populateListView() again will recreate it, but it will be empty again when returned from another activity.
By debugging I found that if I omit startManagingCursor() it works perfectly.
So apparently calling startManagingCursor() twice will cause unexpected si开发者_如何学编程de effects. Why is this happening and how can I resolve it?
Is it the wrong place to call startManagingCursor(), should it rather be in the onCreate() method? And should I actually call stopManagingCursor() at one point?
Is it the wrong place to call startManagingCursor(), should it rather be in the onCreate() method?
This should be fine.
And should I actually call stopManagingCursor() at one point?
When you no longer need the Cursor
to be managed (e.g., you are replacing it), call stopManagingCursor()
.
精彩评论