CursorLoader简单使用例子信息查询

CursorLoader:

         CursorLoader实现异步加载数据,为了避免同步查询数据库时阻塞UI线程的问题。CursorLoader是Loader的子类,Loader可以移步加载数据;loader自己会监视数据源的变化并且会主动上报;当发生配置上的变化,重新生成的loader会自动连接到变化前的cursor,这样就避免再查一次数据库。

 

1、在配置清单里添加查询信息的权限

   <uses-permission android:name="android.permission.READ_SMS"/>

 

 

 

 2、res/layout下2个布局文件activity_main.xml和item_activity.xml布局

 

 

activity_main.xml布局

 

代码

 

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />

</RelativeLayout>

 

 

 

 

======================

 

 

 

item_activity.xml布局

 

代码

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />
    <TextView
        android:id="@+id/text_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />

</LinearLayout>

 

 

 

 

=======================================

 

 

 

3、MainActivity.java类

 

代码

 

 

 

//需要实现接口LoaderCallbacks然后重写接口里的方法
public class MainActivity extends FragmentActivity implements
  LoaderCallbacks<Cursor> {

 private ListView listView;
 private SimpleCursorAdapter adapter;
 private String uri_sms = "content://sms";//访问信息数据库的uri
 private LoaderManager loaderManager;

 private Cursor cursor;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  this.listView = (ListView) this.findViewById(R.id.listview);

  // 最后一个参数 标志 可以用CursorAdapter.出来
  //如果是FLAG_REGISTER_CONTENT_OBSERVER标志 则适配器会在Cursor上注册一个内容观察者
 //该观察者会时时刻刻观察内容的变动 如果通知到达时就调用onContentChanged()方法
  adapter = new SimpleCursorAdapter(this, R.layout.item_activity, cursor,
    new String[] {"body","address"},
    new int[] { R.id.text_phone, R.id.text_content },
    CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
  listView.setAdapter(adapter);
  loaderManager = getSupportLoaderManager();//获取加载管理器
  // initLoader -- 如果当前没有加载器就调用onCreateLoader创建一个 有就根据第一个参数标志重复利用
  // 第一个参数 -- 加载器的标志 随便写
  // 第二个参数 -- Bundle -- 用来传递数据 没有可以写null
  // 第三个参数 -- LoaderCallbacks 对象
  loaderManager.initLoader(1, null, this);
 }

 // ----------继承LoaderCallbacks接口要重写的方法---------------
 @Override// 创建一个Loade
 public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {

  return new CursorLoader(this, Uri.parse(uri_sms), null, null, null, "date desc");
 }

 @Override// Loader创建完成
 public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
  adapter.changeCursor(cursor);
 }

 @Override// 当一个加载器给重置时 调用
 public void onLoaderReset(Loader<Cursor> arg0) {
  adapter.changeCursor(null);
 }
}

 

转载于:https://my.oschina.net/u/2542711/blog/615235

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值