您的位置:首页 > 产品设计 > UI/UE

AsyncQueryHandler

2015-08-20 15:46 405 查看

Android 异步开发之 AsyncQueryHandler

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

[b]AsyncQueryHandler重要的方法:[/b]

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

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

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

[java]
view plaincopy

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异步的几种方式

/article/7505804.html

2.官方文档

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

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

http://download.csdn.net/detail/j1582830/4010012
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: