Cursor.movetonext () will be an exception, as follows
E/androidruntime (2249): FATAL exception:thread-49
E/androidruntime (2249): Java.lang.IllegalStateException:Cannot Perform this operation because the connection pool have be En closed.
E/androidruntime (2249): at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked ( sqliteconnectionpool.java:962)
E/androidruntime (2249): at Android.database.sqlite.SQLiteConnectionPool.waitForConnection ( sqliteconnectionpool.java:599)
E/androidruntime (2249): at Android.database.sqlite.SQLiteConnectionPool.acquireConnection ( sqliteconnectionpool.java:348)
E/androidruntime (2249): at Android.database.sqlite.SQLiteSession.acquireConnection (sqlitesession.java:894)
E/androidruntime (2249): at Android.database.sqlite.SQLiteSession.executeForCursorWindow (sqlitesession.java:834)
E/androidruntime (2249): at Android.database.sqlite.SQLiteQuery.fillWindow (sqlitequery.java:62)
E/androidruntime (2249): at Android.database.sqlite.SQLiteCursor.fillWindow (sqlitecursor.java:143)
E/androidruntime (2249): at Android.database.sqlite.SQLiteCursor.getCount (sqlitecursor.java:133)
E/androidruntime (2249): at Android.database.AbstractCursor.moveToPosition (abstractcursor.java:197)
E/androidruntime (2249): at Android.database.AbstractCursor.moveToNext (abstractcursor.java:245)
Workaround, call Cursor.getcount ().
The reasons are as follows:
when we first call Android.database.sqlite.SQLiteCursor of the GetCount () , the current thread locks the database and unlocks it when the operation is complete.
The call relationship is as follows:At Android.database.sqlite.SQLiteQuery.native_fill_window (native Method)At Android.database.sqlite.SQLiteQuery.Fillwindow (sqlitequery.java:73)At Android.database.sqlite.SQLiteCursor.fillWindow (sqlitecursor.java:287)At Android.database.sqlite.SQLiteCursor.getCount (sqlitecursor.java:268)At Android.widget.CursorAdapter.GetCount(cursoradapter.java:132)If this is the first time you callSqlitecursorof theGetCount()then, in GetCount (), it calls theFillwindow(),in Sqlitecursor'sFillwindow (), it calls Sqlitequery'sFillwindow ()Android.database.sqlite.SQLiteCursorthe relatedThe source code is as follows:@Overridepublic intGetCount () {if(MCount = = No_count) {Fillwindow (0); }returnMCount; }private voidFillwindow (intStartpos) {if(Mwindow = = null) {//If there isn ' t a window set already it would be accessed locally mwindow = new Cursorwindow (True/* The window is local only */); }Else{mcursorstate++; Querythreadlock (); try {mwindow.clear (); } finally {Querythreadunlock (); }} mwindow.setstartposition (Startpos); MCount =Mquery.fillwindow (Mwindow, Minitialread, 0); Return-1 means not finishedif(MCount = = No_count) {MCount = startpos + minitialread; Thread t = new Thread (new Querythread (mcursorstate), "Query Thread"); T.start (); } }in Sqlitequery'sFillwindow (), it first requiresLock Database, and then call the JNI layer'sNative_fill_window ()perform a database operation before the operation is completed.Unlock Database. Android.database.sqlite.SQLiteQuerythe relevant source code is as follows:/*** Reads rows into a buffer. This method acquires the database lock. ** @param window The window to fill in* @return number of total rows in the query */intFillwindow (Cursorwindow window,intMaxread,intLastpos) {LongTimestart = Systemclock.uptimemillis ();Mdatabase.lock ();Mdatabase.logtimestat (MSQL, Timestart, Sqlitedatabase.get_lock_log_prefix);Try{acquirereference ();Try{window.acquirereference ();//If the start POS is not equal to 0 and then most likely windows is//Too small for the data set, loading by another thread//isn't safe in this situation. The native code would ignore MaxreadintNumRows =Native_fill_window(Window, Window.getstartposition (), Moffsetindex, Maxread, Lastpos);//Loggingif(sqlitedebug.debug_sql_statements) {LOG.D (TAG, "Fillwindow ():" + mSql); } mdatabase.logtimestat (MSQL, Timestart); return numrows; }Catch(IllegalStateException e) {//Simply ignore itreturn 0; }Catch(Sqlitedatabasecorruptexception e) {mdatabase.oncorruption (); Throw e; }finally{window.releasereference (); }} finally {releasereference ();Mdatabase.unlock ();} }End it!