激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - 使用CursorLoader異步加載數據

使用CursorLoader異步加載數據

2022-03-09 15:37zzqlivecn Android

這篇文章主要為大家詳細介紹了使用CursorLoader異步加載數據,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

下面是一個例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
 }
}

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

原文鏈接:https://blog.csdn.net/zzqLivecn/article/details/7896297

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 女人久久久www免费人成看片 | 狠狠99 | 一级黄色在线观看 | 爱唯侦察 国产合集 亚洲 | 国产精品久久久免费观看 | 日韩电影av在线 | 精品国产一区二 | 一级做a爱片久久毛片a高清 | 538任你躁在线精品视频网站 | 大学生a级毛片免费视频 | 伊人亚洲精品 | 一区二区久久久久草草 | 日本免费一区二区三区四区 | 日韩精品一二三区 | arabxxxxvideos| 国产精品视频一区二区三区四区五区 | 91av在线免费观看 | 中文字幕精品一二三四五六七八 | 黑人操穴 | 免费毛片电影 | 国产精品久久久久一区二区 | 国产在线91 | 一本色道久久综合亚洲精品图片 | 88xx成人永久免费观看 | 久久要 | 末成年女av片一区二区 | 1000部精品久久久久久久久 | 19禁国产精品福利视频 | 偿还电影免费 | 欧美人与性禽动交精品 | 毛片免费视频观看 | av手机在线电影 | 免费国产在线观看 | 精品国产呦系列在线看 | 日韩黄色免费电影 | 国产扩阴视频 | 久久99国产精品免费网站 | 在线免费av网站 | 亚洲情av| 国产手机在线视频 | 一区二区三区在线观看av |