android sqlite - startManagedCursor (c) versus c.close()?



想知道startManagedCursor()如何作用于/与db.close()和/或cursor.close()确实读过文档,但我仍然对它模糊%-)…并且,了解startManagedCursor()已弃用api>= 11。我认为我必须用它来

代码注释中有两个问题。谢谢!

…SO Rocks:)

Cursor c = null;
try {           
        dbHelper.open() ;
        c = dbHelper.getMyRecords() ;
        startManagingCursor(c) ;
        if ( c.moveToFirst() ) {  uberCool (stuff, here) ; }
        dbHelper.close() ;// <** Question 1 : Is cursor also closed here ?
    } catch (Exception e) {
        Log.d ("OOPS", Caught exception: " + e.toString() ) ;
    } finally {
        // ** Question 2 : Is the close() just below redundant 
        //               - will the managed cursor just close when function
        //                 goes out of scope  ?
        if (c != null) c.close() ; 
    }

我的理解是托管游标由活动拥有。我用了一个在ListActivity里面有ListView的。这样做的主要原因是,当Cursor底层的数据源(在我的例子中是SQLite数据库)中的数据发生变化时,ListView中的视图会自动更新。我认为当管理Activity被杀死时,Cursor是关闭的。在finally块中调用close()可能是多余的。这首先取决于我的假设是正确的,也取决于何时执行此代码。

最新更新