Android 异步开发之 AsyncQueryHandler

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的一个应用:读取电话联系人的数据信息

public class TestAsyncQueryHandler extends Activity {
	
    private static final String NAME = "name", NUMBER = "number", SORT_KEY = "sort_key";
    
    private List<ContentValues> listData;
    private AsyncQueryHandler asyncQuery;  
    
    private ListView personList;
	private BaseAdapter adapter;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        
        setContentView(R.layout.main);  
        
        personList = (ListView) findViewById(R.id.list_view);      
        asyncQuery = new MyAsyncQueryHandler(getContentResolver());
        //异步读取联系人的信息
        asyncQueryContact();       
    }  
  

    private void asyncQueryContact() {
		// TODO Auto-generated method stub
        Uri uri = Uri.parse("content://com.android.contacts/data/phones");  
        String[] projection = { "_id", "display_name", "data1", "sort_key" };  
        asyncQuery.startQuery(0, null, uri, projection, null, null,"sort_key COLLATE LOCALIZED asc");
	}


	private class MyAsyncQueryHandler extends AsyncQueryHandler {  
  
        public MyAsyncQueryHandler(ContentResolver cr) {  
            super(cr);  
  
        }  
  
        @Override  
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {  
            if (cursor != null && cursor.getCount() > 0) {  
            	listData = new ArrayList<ContentValues>();  
                //cursor.moveToFirst();  
                for (int i = 0; i < cursor.getCount(); i++) {
                    ContentValues cv = new ContentValues();  
                    cursor.moveToPosition(i);  
                    String name = cursor.getString(1);  
                    String number = cursor.getString(2);  
                    String sortKey = cursor.getString(3);
                    if (number.startsWith("+86")) {  
                        cv.put(NAME, name);  
                        //process (+86)
                        cv.put(NUMBER, number.substring(3));  
                        cv.put(SORT_KEY, sortKey);  
                    } else {  
                        cv.put(NAME, name);  
                        cv.put(NUMBER, number);  
                        cv.put(SORT_KEY, sortKey);  
                    }  
                    listData.add(cv);  
                }  
                if (listData.size() > 0) {  
                    setAdapter(listData);  
                }  
                cursor.close();
            }  
        }  
  
    }  
  
    private void setAdapter(List<ContentValues> listData) {
    	adapter = new ListAdapter(this, listData);
        personList.setAdapter(adapter);  
  
    }
    
    
    private class ListAdapter extends BaseAdapter {
    	
    	 private LayoutInflater inflater;  
         private List<ContentValues> list;
    	
    	public ListAdapter(Context context, List<ContentValues> list) {
    		this.inflater = LayoutInflater.from(context);
    		this.list = list;   		
    	}
    	
		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {  
                convertView = inflater.inflate(R.layout.list_item, null);
                holder = new ViewHolder();  
                holder.name = (TextView) convertView.findViewById(R.id.name);  
                holder.number = (TextView) convertView.findViewById(R.id.number);  
                convertView.setTag(holder);  
            } else {  
                holder = (ViewHolder) convertView.getTag();  
            }  
			
            ContentValues cv = list.get(position);  
            holder.name.setText(cv.getAsString(NAME));
            holder.number.setText(cv.getAsString(NUMBER));

            return convertView;  
		}
		
		private class ViewHolder { 
            TextView name;  
            TextView number;
		}
    	
    }
     
}  
  

  这个例子,只是使用了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


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值