AsyncQueryHandler
2015-08-20 15:46
405 查看
Android 异步开发之 AsyncQueryHandler
AsyncQueryHandler:官方解释是一个异步帮助类(A helper class to help make handling asynchronous
ContentResolverqueries
easier.) 。这个类的主要作用就是异步对DB数据库进行操作,加快其数据处理的速度(这个非常重要,特别是大容量的数据处理时,例如几千联系人的数据读取,按正常的处理速度会非常的慢,使用AsyncQueryHandler,这就会大大的加快速度,增加用户的良好体验)。
[b]AsyncQueryHandler重要的方法:[/b]
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的一个应用:读取电话联系人的数据信息
[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
相关文章推荐
- uva 10706 Number Sequence
- Android更新UI的方法
- UIStepper使用的具体解释的控制
- easyui的tab切换问题,第一个总显示最后一个
- iOS开发-Day27UISwitch&UISlider&UIStepper&UISegmentedControl
- easy ui 表单元素input控件后面加说明(红色)
- android-UI组件实例大全(七)------Adapter类One之ListView
- easyui icon总结
- HBuilder快捷键
- PAT 1051. Pop Sequence (25)
- Hbuilder检测不到真机的解决方法
- CodeForces-447C DZY Loves Sequences
- 也谈kendoUI的grid.
- Android Call requires API level 11 (current min is 8)的解决方案
- iOS开发-Day26-UILabel&UIButton&UITextField
- LeetCode "Verify Preorder Sequence in Binary Search Tree"
- leetcode - Unique Paths
- serialVersionUID作用
- iOS开发:代码通用性以及其规范 第一篇(附带,自定义UITextView\进度条\双表显示\瀑布流 代码设计思路)
- UE4 Mac 使用Xcode编译教程