使用CursorLoader異步加載數(shù)據(jù)
Android 3.0引入了CursorLoader實現(xiàn)異步加載數(shù)據(jù),為了避免同步查詢數(shù)據(jù)庫時阻塞UI線程的問題。在API 11之前可以通過下載支持庫,來使之前的系統(tǒng)支持此功能。
下面是一個例子:
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 } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android基于Http協(xié)議實現(xiàn)文件上傳功能的方法
這篇文章主要介紹了Android基于Http協(xié)議實現(xiàn)文件上傳功能的方法,結(jié)合實例形式分析了Android的HTTP協(xié)議原理與文件上傳功能實現(xiàn)技巧,需要的朋友可以參考下2016-07-07Android自定義View實現(xiàn)帶音效和震動的SeekBar
這篇文章主要為大家詳細介紹了Android如何自定義View實一個帶音效和震動的SeekBar,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03

Android FlowLayout流式布局實現(xiàn)詳解

Android EditText限制輸入字符類型的方法總結(jié)

Android AlarmManager實現(xiàn)定時循環(huán)后臺任務(wù)