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);
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?