码迷,mamicode.com
首页 > 移动开发 > 详细

Android 异步开发之 AsyncQueryHandler

时间:2015-03-13 20:28:54      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

AsyncQueryHandler

  官方解释是一个异步帮助类(A helper class to help make handling asynchronouContentResolver queries easier.) 。这个类的主要作用就是异步对DB数据库进行操作,加快其数据处理的速度(这个非常重要,特别是大容量的数据处理时,例如几千联系人的数据读取,按正常的处理速度会非常的慢,使用AsyncQueryHandler,这就会大大的加快速度,增加用户的良好体验)。


AsyncQueryHandler重要的方法:

final void startDelete(int token, Object cookie, Uri uri, String selection, String[] selectionArgs)
This method begins an asynchronous delete.
final void startInsert(int token, Object cookie, Uri uri, ContentValues initialValues)
This method begins an asynchronous insert.
void startQuery(int token, Object cookie, Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy)
This method begins an asynchronous query.
final void startUpdate(int token, Object cookie, Uri uri, ContentValues values, String selection, String[] selectionArgs)
This method begins an asynchronous update.
显然,从方法的名字,我们可以看出,这是使用异步方式对DB数据库进行基本的增,删,改,查。

void onDeleteComplete(int token, Object cookie, int result)
Called when an asynchronous delete is completed.
void onInsertComplete(int token, Object cookie, Uri uri)
Called when an asynchronous insert is completed.
void onQueryComplete(int token, Object cookie, Cursor cursor)
Called when an asynchronous query is completed.
void onUpdateComplete(int token, Object cookie, int result)
Called when an asynchronous update is completed.
这几个方法,分别对应于上面四个异步操作时的回调方法。


AsyncQueryHandler的一个应用:读取电话联系人的数据信息

[java] view plaincopy
  1. public class TestAsyncQueryHandler extends Activity {  
  2.       
  3.     private static final String NAME = "name", NUMBER = "number", SORT_KEY = "sort_key";  
  4.       
  5.     private List<ContentValues> listData;  
  6.     private AsyncQueryHandler asyncQuery;    
  7.       
  8.     private ListView personList;  
  9.     private BaseAdapter adapter;    
  10.     
  11.     @Override    
  12.     public void onCreate(Bundle savedInstanceState) {    
  13.         super.onCreate(savedInstanceState);    
  14.           
  15.         setContentView(R.layout.main);    
  16.           
  17.         personList = (ListView) findViewById(R.id.list_view);        
  18.         asyncQuery = new MyAsyncQueryHandler(getContentResolver());  
  19.         //异步读取联系人的信息  
  20.         asyncQueryContact();         
  21.     }    
  22.     
  23.   
  24.     private void asyncQueryContact() {  
  25.         // TODO Auto-generated method stub  
  26.         Uri uri = Uri.parse("content://com.android.contacts/data/phones");    
  27.         String[] projection = { "_id""display_name""data1""sort_key" };    
  28.         asyncQuery.startQuery(0null, uri, projection, nullnull,"sort_key COLLATE LOCALIZED asc");  
  29.     }  
  30.   
  31.   
  32.     private class MyAsyncQueryHandler extends AsyncQueryHandler {    
  33.     
  34.         public MyAsyncQueryHandler(ContentResolver cr) {    
  35.             super(cr);    
  36.     
  37.         }    
  38.     
  39.         @Override    
  40.         protected void onQueryComplete(int token, Object cookie, Cursor cursor) {    
  41.             if (cursor != null && cursor.getCount() > 0) {    
  42.                 listData = new ArrayList<ContentValues>();    
  43.                 //cursor.moveToFirst();    
  44.                 for (int i = 0; i < cursor.getCount(); i++) {  
  45.                     ContentValues cv = new ContentValues();    
  46.                     cursor.moveToPosition(i);    
  47.                     String name = cursor.getString(1);    
  48.                     String number = cursor.getString(2);    
  49.                     String sortKey = cursor.getString(3);  
  50.                     if (number.startsWith("+86")) {    
  51.                         cv.put(NAME, name);    
  52.                         //process (+86)  
  53.                         cv.put(NUMBER, number.substring(3));    
  54.                         cv.put(SORT_KEY, sortKey);    
  55.                     } else {    
  56.                         cv.put(NAME, name);    
  57.                         cv.put(NUMBER, number);    
  58.                         cv.put(SORT_KEY, sortKey);    
  59.                     }    
  60.                     listData.add(cv);    
  61.                 }    
  62.                 if (listData.size() > 0) {    
  63.                     setAdapter(listData);    
  64.                 }    
  65.                 cursor.close();  
  66.             }    
  67.         }    
  68.     
  69.     }    
  70.     
  71.     private void setAdapter(List<ContentValues> listData) {  
  72.         adapter = new ListAdapter(this, listData);  
  73.         personList.setAdapter(adapter);    
  74.     
  75.     }  
  76.       
  77.       
  78.     private class ListAdapter extends BaseAdapter {  
  79.           
  80.          private LayoutInflater inflater;    
  81.          private List<ContentValues> list;  
  82.           
  83.         public ListAdapter(Context context, List<ContentValues> list) {  
  84.             this.inflater = LayoutInflater.from(context);  
  85.             this.list = list;             
  86.         }  
  87.           
  88.         @Override  
  89.         public int getCount() {  
  90.             return list.size();  
  91.         }  
  92.   
  93.         @Override  
  94.         public Object getItem(int position) {  
  95.             return list.get(position);  
  96.         }  
  97.   
  98.         @Override  
  99.         public long getItemId(int position) {  
  100.             return position;  
  101.         }  
  102.           
  103.         @Override  
  104.         public View getView(int position, View convertView, ViewGroup parent) {  
  105.             ViewHolder holder;  
  106.             if (convertView == null) {    
  107.                 convertView = inflater.inflate(R.layout.list_item, null);  
  108.                 holder = new ViewHolder();    
  109.                 holder.name = (TextView) convertView.findViewById(R.id.name);    
  110.                 holder.number = (TextView) convertView.findViewById(R.id.number);    
  111.                 convertView.setTag(holder);    
  112.             } else {    
  113.                 holder = (ViewHolder) convertView.getTag();    
  114.             }    
  115.               
  116.             ContentValues cv = list.get(position);    
  117.             holder.name.setText(cv.getAsString(NAME));  
  118.             holder.number.setText(cv.getAsString(NUMBER));  
  119.   
  120.             return convertView;    
  121.         }  
  122.           
  123.         private class ViewHolder {   
  124.             TextView name;    
  125.             TextView number;  
  126.         }  
  127.           
  128.     }  
  129.        
  130. }    
  

  这个例子,只是使用了AsyncQueryHandler类中的异步查询方法,其它的方法,也是类似,大家可以自己尝试。


源代码下载地址:

           http://download.csdn.net/detail/hfreeman2011/5040647


参考资料:

1.android异步的几种方式

http://ericchan2012.iteye.com/blog/1673929

2.官方文档

http://developer.android.com/reference/android/content/AsyncQueryHandler.html

3.android快速滑动列表 首字母提示

http://download.csdn.net/detail/j1582830/4010012

Android 异步开发之 AsyncQueryHandler

Android 异步开发之 AsyncQueryHandler

标签:

原文地址:http://www.cnblogs.com/lbangel/p/4335859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!