91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用CursorLoader異步加載數據

發布時間:2020-10-10 06:08:25 來源:腳本之家 閱讀:135 作者:zzqlivecn 欄目:移動開發

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

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

饶河县| 如东县| 南丰县| 安平县| 怀远县| 巢湖市| 朝阳市| 博客| 宜宾市| 渝北区| 梁平县| 临海市| 台南市| 惠东县| 涞水县| 洪雅县| 财经| 大石桥市| 靖西县| 湘西| 德惠市| 浙江省| 松滋市| 册亨县| 广南县| 沐川县| 聂荣县| 武宣县| 监利县| 海兴县| 武定县| 星子县| 长岭县| 宿松县| 吕梁市| 聂拉木县| 始兴县| 郸城县| 临汾市| 道孚县| 三门峡市|