Note that there are some explanatory texts on larger screens.

plurals
  1. POA few questions about SQLite database cursors in Android
    primarykey
    data
    text
    <p>To implement database access in my application I followed <a href="http://www.vogella.de/articles/AndroidSQLite/article.html" rel="nofollow">Lars Vogel tutorial</a>, but I'm very confused about a couple of things...</p> <p><strong>1)</strong> Every time a call is made to <code>fetchTodo</code> a new cursor will be created and returned. Leaving the previous cursor for the garbage collector. So, if I don't use <code>startManagingCursor</code> or even the <code>CursorLoader</code> for that matter, should I call a <code>.close()</code> on the cursor when I'm done with it ? Outside of <code>fetchTodo</code> scope of course, example:</p> <pre><code>Cursor cursor = mNotesAdapter.fetchTodo(); // do something... cursor.close(); </code></pre> <p>I'm done with this cursor and new one will be created on the next fetch, should I close it like this then or should I leave it for the garbage collector? Although I think I'm talking about 2 things entirely different... Point being, should I close it like in the example above or not?</p> <p><strong>2)</strong> <code>Cursor</code> also has a <code>.deactivate()</code> method and the documentation says it uses less resources (than active cursors). When exactly should I use this? For instance, in my app, I have a <code>ListActivity</code> which is populated through a <code>SimpleCursorAdapter</code> (the code initialization for this is only called once). The cursor being used is a class member variable, because I need it outside the method that populates the list. I need it to requery the database when something is deleted from it. But until a record is deleted, which is a user action and may take a while to happen, should I deactivate the cursor in the meantime? Cause it will be active again when I call <code>.requery()</code> again. Or the <code>SimpleCursorAdapter</code> is going to stop working because the cursor is not active?</p> <p><strong>EDIT:</strong> I just tested this one and found out that I can't call <code>deactivate()</code> after setting up the cursor adapter. The list will be empty if the cursor is not active, so it needs to remain active for as long as the ListActivity is displayed. In the end, we should just let <code>StartManagingCursor</code> handle it then. Or the new <code>CursorLoader</code>. </p> <p><strong>3)</strong> I know that <code>startManagingCursor</code>/<code>stopManagingCursor</code> are deprecated but I'm not targeting Honeycomb (at least for the time being) and I don't want to deal with the new <code>CursorLoader</code> for now. But in the tutorial above, <code>startManagingCursor</code> is used everywhere, but <code>stopManagingCursor</code> is never called once. Why not? Does Android deals with that in it's own way? Any situation I should call <code>stopManagingCursor</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload