views:

50

answers:

1

Hi everyone,

I'm population my listView using Cursor, but when I navigate away from my activity and then return back my listview is empty. Here is my code:

@Override
public void onCreate(Bundle savedInstanceState) {

...

DBAdapter db = new DBAdapter(context);
db.open();
Cursor c = db.getAll();
db.close();

startManagingCursor(c);
String[] columns = new String[] { ... };
int[] to = new int[] { ... };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_item, c, columns, to);
this.setListAdapter(mAdapter);

...

}

I've seen here questions about saving position of Cursor, but not the Cursor itself. Probably I just missing something, shall I save my cursor (how can I do it?) or it's better(faster, cheaper) to create new cursor every time using my DBadapter?

Thanks

+3  A: 

startManagingCursor() makes your close() call unnecessary. As long as you didnt get exceptions about not finalizing or closing your cursor you have done anything right.

WarrenFaith
Do I need to take any other actions to make sure the connection is closed and memory is freed? e.g. add something to `onPause()` or something similar? thanks
Burjua
It is all mentioned in the documentation: This method allows the activity to take care of managing the given Cursor's lifecycle for you based on the activity's lifecycle. That is, when the activity is stopped it will automatically call `deactivate()` on the given Cursor, and when it is later restarted it will call `requery()` for you. When the activity is destroyed, all managed Cursors will be closed automatically.
WarrenFaith
Ok, thank you for your time))
Burjua