Use AsyncQueryHandler and CursorAdapter, simplecursoradapter
AsyncQueryHandler
A helper class to help make handling asynchronousContentResolverQueries easier.
A helper class to help simplify the processing of asynchronous ContentResolver queries.
Application scenarios of AsyncQueryHandler:
In general applications, you can use ContentProvider to operate databases.
This is no problem when the data volume is small, but if the data volume is large, it may cause ANR exceptions in the UI thread (more than 5 seconds ).
Of course, you can also write a Handler to perform these operations, but you need to write another Handler every time you use ContentProvider, which will inevitably reduce the efficiency.
Therefore, when the data volume is large, it is best to use the asynchronous query framework AsyncQueryHandler encapsulated by Android to optimize our code.
Note that CursorAdapter is usually used to query data of local applications.
Internal Implementation of AsyncQueryHandler
The AsyncQueryHandler class encapsulates the interaction process between the caller thread and the worker thread. The interaction subject is two Handler, one running in the caller thread and the other running in the worker thread. The onXXXComplete callback interface is provided to complete event processing.
How to Use AsyncQueryHandler
Create a new class that inherits the AsyncQueryHandler class and provides the onXXXComplete method (either one or more can be implemented, but you can also do not, if you do not pay attention to the results of database operations), perform some operations on the database in your implementation.
Call the startXXX method directly. The passed general parameters are as follows:
Token, a token used to identify the query and ensure it is unique. It must be consistent with the onXXXComplete method. (Of course, you can also be inconsistent. The corresponding onXXXComplete method will be called after the database operation is completed)
Cookie, which is an object that you want to pass to the onXXXComplete method. (If not, pass null)
Uri uri (generic resource identifier used for query ):
Column queried by projection
Selection restrictions
SelectionArgs query parameters
OrderBy sorting Condition
After the method is called, the corresponding operation is performed asynchronously. After the operation is completed, the onQueryComplete (int token, Object cookie, Cursor cursor) method is called back. The returned cursor is usually set to the adapter in this method. You can also set some listeners in this method to perform corresponding operations.
/**
* Asynchronous query operation help class, which can process addition, deletion, and modification (data provided by ContentProvider)
* @ Author HelloWorld
*
*/
Public class CommonQueryHandler extends AsyncQueryHandler {
Public CommonQueryHandler (ContentResolver cr ){
Super (cr );
}
/**
* Callback method after the query is complete
*/
@ Override
Protected void onQueryComplete (int token, Object cookie, Cursor cursor ){
// Determine whether the passed cookie is a CursorAdapter
If (cookie! = Null & cookie instanceof CursorAdapter ){
CursorAdapter adapter = (CursorAdapter) cookie;
// Set the returned cursor to the adapter
Adapter. changeCursor (cursor );
}
// Trigger the listener event
If (cursorChangedListener! = Null ){
CursorChangedListener. onCursorChanged (token, cookie, cursor );
}
}
Public OnCursorChangedListener getCursorChangedListener (){
Return cursorChangedListener;
}
Public void setOnCursorChangedListener (OnCursorChangedListener cursorChangedListener ){
This. cursorChangedListener = cursorChangedListener;
}
Private OnCursorChangedListener cursorChangedListener;
/**
* Define event listening when cursor changes
* @ Author leo
*
*/
Public interface OnCursorChangedListener {
Void onCursorChanged (int token, Object cookie, Cursor cursor );
}
}
CursorAdapter
Adapter that exposes data fromCursorToListViewWidget. The Cursor must include a column named "_ id" or this class will not work.
This class can be usedCursorAccess the database and display the queried data to the List View (ListView) Component. The result set carried by the cursor must contain a column named "_ id". Otherwise, this class cannot work.
Common CursorAdapter methods:
Public CursorAdapter (Context context, Cursor c)
The constructor of CursorAdapter. The first parameter is passed into the context, and the second parameter is usually null.
Public abstract View newView (Context context, Cursor cursor, ViewGroup parent)
Makes a new view to hold the data pointed to by cursor.
Generally, the ItemView of ListView is initialized here, and the control is initialized.
Public abstract void bindView (View view, Context context, Cursor cursor)
Bind an existing view to the data pointed to by cursor
Bind data to the ItemView control, and you can use cursor to directly obtain the data to be queried.