I have a problem understanding why is my ListActivity and its corresponding listView empty when I call close() method on cursor object?
Let me explain myself a bit...
I retreive some values from a DB and get the result in my cursor object. After that I create my SimpleCursorAdapter and bind column from db with a field in my listView.
Works like a charm, but...
If I call cursor.close() at the end of onCreate() method my listView is shown empty?
If I log the values from cursor to LogCat they're there until calling cursor.close() and that makes perfect sense, but why is the listAdapter emptied when cursor.close() is called??? I would expect ListAdapter listAdapter = new SimpleCursorAdapter(...) to hold the values that are "binded" to a listView until we activity is destroyed...
Why is this so? When and why is necessary to close cursor?
public class ListTrainingsView extends ListActivity{
private SQLiteDatabase db;
private ListAdapter listAdapter;
private Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_trainings);
DatabaseHelper helper = new DatabaseHelper(this);
this.db = helper.getWritableDatabase();
this.cursor = db.query("trainings", fields, null, null, null, null, null);
startManagingCursor(cursor);
this.listAdapter = new SimpleCursorAdapter(this,
R.layout.list_trainings_item,
cursor,
new String[] {"name"},
new int[] { R.id.name_entry}
);
this.setListAdapter(this.listAdapter);
//If I call
//cursor.close();
//shown list is empty
Another question is more of basic Java language type of question... I come from PHP OO background and there if you define member variable you have to work with it in object methods using syntax $this->cursor. I've noticed that in Android/Java I don't have to use this.cursor.getCount() to get the reference/value from it. It's enough to say cursor.getCount() How come this is allowed?
Why is this so?
Adapter
needs data from provided Cursor
and prepare ListView
accordingly, and when you call cursor.close()
the Cursor
is released and made invalid. (means there is no data.)
When and why is necessary to close cursor?
It is necessary to close the Cursor
while you are about to leave the Activity
and going back from the Activity
otherwise Cursor
got leaked for that Activity
.
- References From documentation
- Any why it is necessary
It's enough to say cursor.getCount() How come this is allowed?
The structure of Java is based on classes. Every action is done within a specific class. Even echoing "single line" would need a class. and when you are in a class you can access class members with this
OR with direct their variables
- For more see here
You should not close your cursor unil your Activity is destroyed (i.e. in onDestroy()
) because the implementation of CursorAdapter expects it to be open to be able to requery and filter it.
Since you're calling startManagingCursor
anyway, your Activity will automatically deactivite, requery and close the cursor upon the appropriate Activity lifecycle events, so there is no need to close it yourself.
来源:https://stackoverflow.com/questions/7901578/listview-empty-if-simplecursoradapter-closed