Android query & managedquery

Source: Internet
Author: User

Let's talk about the following two methods to query data:

 

 

 

Java code
  1. Cursor C1 = mcontext. getcontentresolver (). Query (Uri, projection, selection, selectionargs, sortorder );
  2. 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
  1. E/cursor (1384): finalizing a cursor that has not been deactivated or closed. database =/data/XX/databases/xx. DB, table = XX, query = select xx
  2. E/cursor (1384): Android. database. SQLite. databaseobjectnotclosedexception: application did not close the cursor or database object that was opened here
  3. E/cursor (1384): at Android. database. SQLite. sqlitecursor. <init> (sqlitecursor. Java: 210)
  4. E/cursor (1384): at Android. database. SQLite. sqlitedirectcursordriver. Query (sqlitedirectcursordriver. Java: 53)
  5. E/cursor (1384): at Android. database. SQLite. sqlitedatabase. rawquerywithfactory (sqlitedatabase. Java: 1345)
  6. E/cursor (1384): at Android. database. SQLite. sqlitedatabase. querywithfactory (sqlitedatabase. Java: 1229)
  7. E/cursor (1384): at Android. database. SQLite. sqlitedatabase. Query (sqlitedatabase. Java: 1184)
  8. 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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.