国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

使用CursorLoader異步加載數據

2019-10-21 21:47:59
字體:
來源:轉載
供稿:網友

Android 3.0引入了CursorLoader實現異步加載數據,為了避免同步查詢數據庫時阻塞UI線程的問題。在API 11之前可以通過下載支持庫,來使之前的系統支持此功能。

下面是一個例子:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 旅游| 合川市| 宁乡县| 当雄县| 哈巴河县| 蓬安县| 阜南县| 金昌市| 两当县| 台北市| 榆林市| 噶尔县| 玉环县| 凌海市| 舒城县| 商城县| 吉首市| 诸城市| 岱山县| 阜康市| 平远县| 陈巴尔虎旗| 马龙县| 庆云县| 稷山县| 格尔木市| 高台县| 广安市| 商南县| 云龙县| 扶绥县| 固原市| 锦屏县| 鄂州市| 抚州市| 山西省| 常宁市| 蓬莱市| 元谋县| 芦山县| 隆安县|