Android 3.0引入了CursorLoader实现异步加载数据,为了避免同步查询数据库时阻塞UI线程的问题。在API 11之前可以通过下载支持库,来使之前的系统支持此功能,下载页面为

http://developer.android.com/tools/extras/support-library.html

下面是一个例子:

public class ListViewLoader extends ListActivity
implements LoaderManager.LoaderCallbacks<Cursor> { // This is the Adapter being used to display the list's data
SimpleCursorAdapter mAdapter; // These are the Contacts rows that we will retrieve
static final String[] PROJECTION = new String[] {ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME}; // This is the select criteria
static final String SELECTION = "((" +
ContactsContract.Data.DISPLAY_NAME + " NOTNULL) AND (" +
ContactsContract.Data.DISPLAY_NAME + " != '' ))"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Create a progress bar to display while the list loads
ProgressBar progressBar = new ProgressBar(this);
progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, Gravity.CENTER));
progressBar.setIndeterminate(true);
getListView().setEmptyView(progressBar); // Must add the progress bar to the root of the layout
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
root.addView(progressBar); // For the cursor adapter, specify which columns go into which views
String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};
int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1 // Create an empty adapter we will use to display the loaded data.
// We pass null for the cursor, then update it in onLoadFinished()
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, null,
fromColumns, toViews, 0);
setListAdapter(mAdapter); // Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
} // Called when a new Loader needs to be created
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
PROJECTION, SELECTION, null, null);
} // Called when a previously created loader has finished loading
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
} // Called when a previously created loader is reset, making the data unavailable
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
} @Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something when a list item is clicked
}
}

最新文章

  1. A log about Reading the memroy of Other Process in C++/WIN API--ReadProcessMemory()
  2. 细说IIS异常日志 — 你必须知道的功能
  3. 分佈式事務故障處理暨ORA-24756: transaction does not exist處理
  4. 带你学C,带你飞——入门
  5. cocos2d-x 如何保持屏幕常亮
  6. 对shell中的与(&amp;&amp;)和或(||)的理解
  7. mongoDB知识总结
  8. 练习SignalR使用
  9. DM8168硬件平台
  10. 值为NULL的对象指针
  11. QEMU ELF_LOAER分析[基于MIPS]
  12. MTVERIFY
  13. 开源负载均衡通讯分发器(LB dispatcher) - G5
  14. 3d-tiles、gltf 坐标系
  15. SpringBoot 项目打包后运行报 org.apache.ibatis.binding.BindingException
  16. Codeforces 1019C Sergey&#39;s problem 构造
  17. 未能加载文件或程序集“Benlai.SOA.Framework.Common, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。
  18. Linux /etc/cron.d作用(转自 定时任务crontab cron.d)
  19. oozie安装总结
  20. 「TJOI2013」攻击装置

热门文章

  1. JS判断客户端是否是iOS或者Android手机移动端
  2. IE8下面的line-height的bug
  3. OpenCL学习笔记(一):摩尔定律,异构计算与OpenCL初印象
  4. string与int的相互转换以及把一个字符加入到string的末尾
  5. Internet Explorer 浏览器在同一时刻只能从同一域名下载两个文件。
  6. “无法识别的配置节system.webServer”解决办法
  7. kubernetes如何要使用用户名和密码登陆harbor以拉取docker镜像,应该如何操作?
  8. Apache配置基于IP的虚拟主机 Apache virtual host configuration is based on the IP
  9. loadrunner中文件的操作
  10. mybatis 报错: Invalid bound statement (not found)