ListView is empty if SimpleCursorAdapter is closed ()

I have a problem understanding why my ListActivity and its corresponding listView is empty when I call the close () method on a cursor object?

Let me explain a little ...

I return some values ​​from the database and get the result in my cursor object. After that, I create my SimpleCursorAdapter and bind the db column to a field in my View list.

It works like a charm, but ...

If I call cursor.close () at the end of the onCreate () method, is my View list empty?

If I write the values ​​from the cursor to LogCat, they are there before calling cursor.close (), and that makes sense, but why is the listAdapter empty when cursor.close () is called ??? I would expect ListAdapter listAdapter = new SimpleCursorAdapter (...) to hold the values ​​tied to the View list until we destroy the action ...

Why is this so? When and why do you need to close the 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 the question of the main type of the Java language ... I came from the background of PHP OO and there, if you define a member variable, you need to work with it in object methods using the syntax $ this-> cursor. I noticed that in Android / Java I don't need to use this.cursor.getCount () to get the link / value. This is enough to say cursor.getCount () How is this allowed?

+4
source share
2 answers

Why is this so?

Adapter needs data from Cursor and prepares ListView accordingly, and when cursor.close() called, Cursor freed up and becomes invalid. (means no data.)

When and why do you need to close the cursor?

You must close Cursor when you are about to leave the Activity and return from the Activity otherwise Cursor received a leak for this Activity .

Just say cursor.getCount () How is this allowed?

The Java structure is based on classes. Each action is performed in a specific class. Even a single line echo needs a class. and when you are in a class, you can access class members using this OR with their direct variables

+1
source

You should not close the cursor until your activity is destroyed (i.e. in onDestroy() ), because the CursorAdapter implementation expects it to be open in order to be able to query and filter it.

Since you still call startManagingCursor , your activity automatically deactivates, requests, and closes the cursor to the corresponding events in the Activity life cycle, so there is no need to close it yourself.

+2
source

All Articles