使用CursorLoader异步加载数据
作者:zzqlivecn 时间:2021-10-06 12:17:31
Android 3.0引入了CursorLoader实现异步加载数据,为了避免同步查询数据库时阻塞UI线程的问题。在API 11之前可以通过下载支持库,来使之前的系统支持此功能。
下面是一个例子:
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
}
}
来源:https://blog.csdn.net/zzqLivecn/article/details/7896297
标签:cursorLoader,异步加载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#操作ftp类完整实例
2021-09-02 22:45:30
详解SpringBoot 快速整合Mybatis(去XML化+注解进阶)
2022-02-19 03:54:29
![](https://img.aspxhome.com/file/2023/9/65359_0s.png)
Java实现的基于socket通信的实例代码
2021-12-30 19:06:50
C# TrieTree介绍及实现方法
2022-02-10 22:04:53
![](https://img.aspxhome.com/file/2023/8/122718_0s.jpg)
通过Session案例分析一次性验证码登录
2023-04-27 06:25:51
java操作json对象出现StackOverflow错误的问题及解决
2023-03-04 20:06:14
![](https://img.aspxhome.com/file/2023/2/63162_0s.png)
HashMap在JDK7与JDK8中的实现过程解析
2022-03-04 18:26:44
![](https://img.aspxhome.com/file/2023/7/132117_0s.png)
Android数据传输中的参数加密代码示例
2021-05-25 23:54:43
java 发送邮件的实例代码(可移植)
2022-09-23 15:53:44
C# 对象映射的高性能方案
2021-09-22 08:03:19
![](https://img.aspxhome.com/file/2023/6/104296_0s.png)
C#中SQL Command的基本用法
2023-10-04 06:01:32
JDK动态代理,代理接口没有实现类,实现动态代理方式
2021-12-21 11:55:22
详解Spring循环依赖的解决方案
2022-05-29 13:14:57
SpringMVC一步到位精通拦截器
2023-11-25 01:47:45
![](https://img.aspxhome.com/file/2023/1/60081_0s.png)
使用mybatis-plus想要修改某字段为null问题
2021-09-17 02:17:35
Android实现背景可滑动登录界面 (不压缩背景弹出键盘)
2023-11-14 11:09:07
![](https://img.aspxhome.com/file/2023/9/138469_0s.gif)
emoji表情与unicode编码互转的实现(JS,JAVA,C#)
2023-02-07 06:07:48
Java的锁机制:synchronized和CAS详解
2023-03-18 16:04:00
![](https://img.aspxhome.com/file/2023/9/101929_0s.png)
Java内存模型可见性问题相关解析
2023-01-15 06:22:06
Stream distinct根据list某个字段去重的解决方案
2022-06-22 22:23:26
![](https://img.aspxhome.com/file/2023/0/62380_0s.png)