Note that there are some explanatory texts on larger screens.

plurals
  1. POHow correctly to close the cursor Android Java
    text
    copied!<p>If I try to close the cursor with <code>onSets.close();</code> in method <code>getData()</code>, the data does not appear in the position. If I do not close the cursor method, then Eclipse complains and says that the base or the cursor is not closed. But if I add a line <code>c.moveToFirst();</code> in the method <code>getSets(String toexes_ids, String toprog_dif)</code>, the error does not appear. Why is that? I have two method:</p> <p>One method in MyDatabase.java</p> <pre><code>public Cursor getSets(String toexes_ids, String toprog_dif) { SQLiteDatabase db = getReadableDatabase(); SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); String [] sqlSelect = {"_id", "exes_ids", "sets_ids", "sets_weight", "sets_ones"}; String sqlTables = "Sets"; String selection = "exes_ids = " + toexes_ids + " AND " + "prog_dif = " + toprog_dif; qb.setTables(sqlTables); Cursor c = qb.query(db, sqlSelect, selection, null, null, null, null); c.moveToFirst(); return c; } </code></pre> <p>Second method in MainActivity.java</p> <pre><code>public void getData() { db = new MyDatabase(this); onSets = db.getSets(toexes_ids, toprog_dif); ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.itemsets, onSets, new String[] {"sets_ids", "sets_weight", "sets_ones"}, new int[] {R.id.itemsets_ids, R.id.itemsets_weight, R.id.itemsets_ones}); listSets.setAdapter(adapter); db.close(); } </code></pre> <p>Error:</p> <pre><code>12-29 16:17:09.335: D/dalvikvm(571): GC_CONCURRENT freed 393K, 53% free 2759K/5767K, external 1574K/1802K, paused 5ms+10ms 12-29 16:17:09.355: E/Database(571): close() was never explicitly called on database '/data/data/com.exp.exp_betta/databases/fitsdbsqlite3' 12-29 16:17:09.355: E/Database(571): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 12-29 16:17:09.355: E/Database(571): at android.database.sqlite.SQLiteDatabase.&lt;init&gt;(SQLiteDatabase.java:1847) </code></pre>
 

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