[Android Pro] 使用CursorLoader非同步載入資料 from 3.0

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   使用   ar   

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    }}

 

[Android Pro] 使用CursorLoader非同步載入資料 from 3.0

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.