AsyncQueryHandler:
官方解释是一个异步帮助类(A helper class to help make handling asynchronous ContentResolver
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.
|
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