1

If I have a SimpleCursorAdapter and I call getCursor() on my instantiated adapter, should I then close this cursor when I'm finished with it, since java is pass-by-value?

Simple example:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(.....);
myListView.setAdapter(adapter);

Cursor cursor = adapter.getCursor();
cursor.moveToFirst();
int id = cursor.getInt(0);
...?

If I close the cursor here, will it be closed for the adapter or not?

ZNS
  • 840
  • 1
  • 9
  • 14

1 Answers1

2

If I have a SimpleCursorAdapter and I call getCursor() on my instantiated adapter, should I then close this cursor when I'm finished with it, since java is pass-by-value?

As you said, the Adapter is still using the Cursor, so no, you should not close it. You should only close the Cursor when you are completely finished working with it.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • My question is more regarding if the cursor returned points to the same cursor as the adapter uses, because of pass-by-value. But I guess pass-by-value doesn't apply for getting properties? – ZNS Dec 10 '12 at 21:00
  • Yes, it is the same Cursor. You can always save a local reference on the Cursor you pass to `new SimpleCursorAdapter()` and test `cursor == adapter.getCursor()`. – Sam Dec 10 '12 at 21:05
  • As a note, be careful with how you use `==` in Java... Only use it to compare primitive objects (not including Strings) and _instances_ (like above). If this is new to you read [How do I compare strings in Java?](http://stackoverflow.com/q/513832/1267661) for a detailed explanation on `==` and `equals()`. – Sam Dec 10 '12 at 21:57