18 Loader代碼案例

  • 目錄結構:
    這裏寫圖片描述java

  • MainActivity.java 代碼:android

    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 markdown

    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.xmlapp

    <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.xmlide

    <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>
相關文章
相關標籤/搜索