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. Android的线程Handler实现
  2. android 访问网络不能在主线程中进行以及在线程中操作UI的解决方
  3. Android-Jni线程(二)— 线程锁之生产者消费者
  4. Android利用Looper在子线程中改变UI
  5. android 中,关于线程安全退出的问题(from stack overflow)
  6. 多线程实现android更新进度条
  7. Android中子线程网络查看器与Handler消息处理器
  8. kotlin 开发 android 程序中网络http请求和线程的使用

随机推荐

  1. Android(安卓)开发值得订阅的日报和周刊
  2. Android studio 开发第一篇 APP项目创建
  3. Android SSL 过程记录(证书的生成+例子代
  4. 超简单的几行代码搞定Android底部导航栏
  5. Android 绘图机制:canvas初解
  6. android 环境变量配置,以及sdcard配置
  7. Android L中的RecyclerView 、CardView
  8. 【Android】第三方QQ账号登录的实现
  9. My first app on Android Market -- 24-G
  10. WP7和Android控件对照表