title: Closing Cursor in function that returns Cursor
body: <p>I have a problem with closing Cursor in my SQLite class. When I'll close the Cursor and <code>SQLiteDatabase</code> in finally block (in <code>DBHelper</code>) then i can't read data in my other class (Can't reopen closed Cursor error) but when I do not close Cursor and <code>SQLiteDatabase</code> then i have memory leak errors.</p>
  
  <p>DHHelper.java :</p>
  
  <pre><code>//...
  public Cursor selectData(String selectVal, String tabName){
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor c = null;
      try {
          String areaTyp = "SELECT " + selectVal + "  FROM " + tabName;
          c = db.rawQuery(areaTyp, null);
          return c;
      } finally {
          c.close();
          db.close();
      }
  }
  //...
  </code></pre>
  
  <p>UserMenager.java</p>
  
  <pre><code>    public List&lt;UserModel&gt; getUsersList(){
      List&lt;UserModel&gt; allUsers = new ArrayList&lt;UserModel&gt;();
  
      final DBHelper db = new DBHelper(appStatic.CONTEX);
      Cursor c = db.selectData("*","User");
      if (c.moveToFirst()) {
          while (!c.isAfterLast()) {
  
              allUsers.add(new UserModel(c.getString(c.getColumnIndex("cms_id")),
                      c.getString(c.getColumnIndex("login")),
                              c.getString(c.getColumnIndex("name")),
                                      c.getString(c.getColumnIndex("active")),
                                              c.getString(c.getColumnIndex("avatar")),
                                                      c.getString(c.getColumnIndex("gtoken"))));
  
              c.moveToNext();
          }
      }
      c.close();
      db.close();
  
      return allUsers;
  }
  </code></pre>
  
  <p>Maybe I should do this in other way and the function that returns Cursor is a bad idea? </p>
  
  <p><strong>EDIT:</strong>
  When i run app, i have error:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: attempt to re-open an already-closed
    object: SQLiteQuery: SELECT *  FROM User</p>
  </blockquote>
  
  <p>In UserMenager.java, line</p>
  
  <pre><code>if (c.moveToFirst()) {
  </code></pre>