Asynchronous query-----Asyncqueryhandler for databases

Source: Internet
Author: User

Using the Asyncqueryhandler Asynchronous query helper class, you can implement the database additions and deletions (data provided by Contentprovidert). If the amount of data is small, the use of General data query can be achieved. However, if the amount of data is large, there will be the ANR (application not responding) problem, you can also use handler, but this will reduce efficiency, this time we can use Android an already encapsulated asynchronous query framework Asyncqueryhandler .

Asyncqueryhandler Internal implementation: TheAsyncqueryhandler class encapsulates the process of interaction between the caller thread and the worker thread. The subject of the interaction is two handler, one running in the caller thread and one running in the worker thread. Creates a Asyncqueryhandler object and implements the completion of the event by providing a callback interface to the Onxxxcomplete.


There are four methods available in the Android API:

Startinsert: inserting

Startdelete: Delete

Startupdate: Update

Startquery: Query

The four methods, and the response of the Onxxxcomplete () method correspond to it. The corresponding 4 Onxxxcomplete () methods are NULL implementations, so after we complete the corresponding call, we can do the following The Onxxxcomplete () method is completed .


How to use

Inherit the Asyncqueryhandler class, and provide the implementation of the Onxxxcomplete method (you can implement any one or more, of course you can also not implement, if you do not care about the results of the operation database), Do some processing of the database operation in your implementation.

Call the Startxxx method directly when you use it. The common parameters passed in are as follows:

Token: A token that is used primarily to identify the query and ensure that it is unique. needs to be consistent with the Onxxxcomplete method. (Of course, you can also be inconsistent, also call the corresponding Onxxxcomplete method at the end of the database operation)

Cookie: You want to pass to an object that is used by the Onxxxcomplete method. (pass NULL if not)

URI URI (common resource identifier for query):

Projection the columns of the query

Selection restriction conditions

Selectionargs Query Parameters

Sort by order criteria

Let's take a look at an example of a query using asyncqueryhandler :

Must have _id field final string[] From=new string[] {"_id", "id"};//database Asynchronous query new Asyncqueryhandler (Getcontentresolver ()) { protected void Onquerycomplete (int token, Object cookie,cursor Cursor) {// Testadapter is a subclass of Simplecursoradapter testadapter adapter = new Testadapter (mainactivity.this, Cursor,from); Listview.setadapter (adapter); Listview.setselection (Adapter.getcount ()-1);};}. Startquery (0, NULL, Testprovider.content_uri, from,null, NULL, NULL);

Abovemust have _id field"For the reason I don't say much here, please refer to my other blog: click Open Link http://blog.csdn.net/u014544193/article/details/40976579.Testprovider is a subclass of ContentProvider.

Asyncqueryhandler Introduction, to Here is basically the end, I hope to have some help group!!!

Asynchronous query-----Asyncqueryhandler for databases

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.