Let's talk about the following two methods to query data:
Java code
- Cursor C1 = mcontext. getcontentresolver (). Query (Uri, projection, selection, selectionargs, sortorder );
- Cursor C2 = mactivity. managedquery (Uri, projection, selection, sortorder );
Method 1: If the cursor C1 data is put into the adapter, for example, if getview () is called to load data, you need to control the cursor in the activity lifecycle corresponding to the adapter, pay special attention to the processing of c1.close (). Otherwise, the following exception will occur repeatedly.
Java code
- E/cursor (1384): finalizing a cursor that has not been deactivated or closed. database =/data/XX/databases/xx. DB, table = XX, query = select xx
- E/cursor (1384): Android. database. SQLite. databaseobjectnotclosedexception: application did not close the cursor or database object that was opened here
- E/cursor (1384): at Android. database. SQLite. sqlitecursor. <init> (sqlitecursor. Java: 210)
- E/cursor (1384): at Android. database. SQLite. sqlitedirectcursordriver. Query (sqlitedirectcursordriver. Java: 53)
- E/cursor (1384): at Android. database. SQLite. sqlitedatabase. rawquerywithfactory (sqlitedatabase. Java: 1345)
- E/cursor (1384): at Android. database. SQLite. sqlitedatabase. querywithfactory (sqlitedatabase. Java: 1229)
- E/cursor (1384): at Android. database. SQLite. sqlitedatabase. Query (sqlitedatabase. Java: 1184)
- E/cursor (1384): at Android. database. SQLite. sqlitedatabase. Query (sqlitedatabase. Java: 1301)
Method 2: cursor C2 allows the activity to control the lifecycle of C2 as the activity lifecycle changes. Therefore, you do not need to worry about whether the cursor is closed or requery.
Reference: http://consuela-zhao.iteye.com/blog/961617