18 Loader代码案例

  • 目录结构:
    这里写图片描述

  • MainActivity.java 代码:

    package com.qf.day18_loader_demo2;
    
    import android.app.Activity;
    import android.app.LoaderManager.LoaderCallbacks;
    import android.content.Loader;
    import android.database.ContentObserver;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.widget.ListView;
    import android.widget.SearchView;
    import android.widget.SearchView.OnQueryTextListener;
    import android.widget.SimpleCursorAdapter;
    
    public class MainActivity extends Activity implements LoaderCallbacks<Cursor>{
    
        private ListView lv;
        private SimpleCursorAdapter adapter;
    
        private SearchView sv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            lv = (ListView) findViewById(R.id.lv);
            sv = (SearchView) findViewById(R.id.sv);
    
    
            sv.setOnQueryTextListener(new OnQueryTextListener() {
    
                @Override
                public boolean onQueryTextSubmit(String query) {
                    // TODO Auto-generated method stub
                    return false;
                }
    
                @Override
                public boolean onQueryTextChange(String newText) {
                    // TODO Auto-generated method stub
    
                    Bundle bundle = new Bundle();
                    bundle.putString("key", newText.toString());
    
                    getLoaderManager().restartLoader(1, bundle, MainActivity.this);
                    return false;
                }
            });
    
    
            adapter = new SimpleCursorAdapter(
                    MainActivity.this, R.layout.item,null, 
                    new String[]{"address","body"},
                    new int[]{R.id.tv_adress,R.id.tv_body},
                    SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    
    
            lv.setAdapter(adapter);
    
            //初始化Loader对象 
            getLoaderManager().initLoader(1, null, this);
    
            /**
             * 注册观察者对象    给当前Uri
             * 参数1:Uri对象
             * 参数2:   如果返回true    检测到 content:sms    content:sms/address   content:sms/address/iii
             *        如果返回false    只能检测到当前的content:sms    
             *        
             * 参数3:观察者
             * 
             */
            getContentResolver().registerContentObserver(
                    Uri.parse("content://sms"), true, new MyContentObserver(null));
        }
    
    
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            // 实例化Loader对象  并返回  
            SmsLoader smsLoader = new SmsLoader(MainActivity.this,args);
            return smsLoader;
            //看个人喜好情况使用 两种方式 如果使用以下方式 请删除上面的
    //       Uri smsUri = Uri.parse("content://sms");
    //       String[] colums = {"_id","address","body"};
    //       String selection = null;
    //       String[] selectionArgs = null;
    //      
    //      CursorLoader cursorLoader = new CursorLoader(MainActivity.this, smsUri, colums, selection, selectionArgs, null);
    //      return cursorLoader;
        }
    
        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            // TODO Auto-generated method stub
            adapter.swapCursor(data);
        }
    
        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            // TODO Auto-generated method stub
            adapter.swapCursor(null);
        }
    
    
        /**
         * 自定义的观察者
         * @author sxy
         *
         */
        class MyContentObserver extends ContentObserver{
    
            public MyContentObserver(Handler handler) {
                super(handler);
                // TODO Auto-generated constructor stub
            }
    
            //当你的数据发生改变时  调用此方法
            @Override
            public void onChange(boolean selfChange) {
                // TODO Auto-generated method stub
                super.onChange(selfChange);
                //重启Loader
                getLoaderManager().restartLoader(1, null, MainActivity.this);
            }
    
        }
    
    
    
    }
    
  • SmsLoader.java

    package com.qf.day18_loader_demo2;
    
    import android.content.AsyncTaskLoader;
    import android.content.Context;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    
    /**
     * 自定义的AsyncTaskLoader
     *
     *
     */
    public class SmsLoader extends AsyncTaskLoader<Cursor> {
    
        private Uri smsUri = Uri.parse("content://sms");
        private String[] colums = {"_id","address","body"};
        private String selection = null;
        private String[] selectionArgs = null;
    
    //  private Bundle bundle;
    
        public SmsLoader(Context context,Bundle bundle) {
            super(context);
            // TODO Auto-generated constructor stub
    //      this.bundle = bundle;
            if(bundle!=null){
                selection = "body like ?";
                selectionArgs = new String[]{"%"+bundle.getString("key")+"%"};
            }
        }
    
        /**
         * Ui线程执行
         */
        @Override
        protected void onStartLoading() {
            // TODO Auto-generated method stub
            super.onStartLoading();
    
            Log.e("AAA", "==>"+Thread.currentThread().getName());
            forceLoad();//必须执行    强制向下执行 不然此类无效
        }
    
        /**
         * 耗时操作  工作线程   可以执行查询操作
         */
        @Override
        public Cursor loadInBackground() {
            // TODO Auto-generated method stub
    
    
    
            Log.e("AAA", "==>"+Thread.currentThread().getName());
    //      Log.e("AAA", "=selection=>"+selection);
    //      Log.e("AAA", "=selectionArgs=>"+selectionArgs[0]);
            Cursor cursor = getContext().getContentResolver().query(
                    smsUri, colums, selection, selectionArgs, null);
    
            return cursor;
        }
    
    
    
    
    
    }
    
  • activity_main.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"
        tools:context=".MainActivity" >
    
        <SearchView 
            android:id="@+id/sv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ></SearchView>
    
        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
  • item.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"
        tools:context=".MainActivity" >
    
        <SearchView 
            android:id="@+id/sv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ></SearchView>
    
        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>

posted on 2016-09-14 12:13  木鱼哥  阅读(119)  评论(0编辑  收藏  举报

导航