使用CursorLoader异步加载数据

Android 3.0引入了CursorLoader实现异步加载数据,为了避免同步查询数据库时阻塞UI线程的问题。在API 11之前可以通过下载支持库,来使之前的系统支持此功能,下载页面为

http://developer.android.com/tools/extras/support-library.html

下面是一个例 子:

View Code
 1 public class ListViewLoader extends ListActivity

 2         implements LoaderManager.LoaderCallbacks<Cursor> {

 3 

 4     // This is the Adapter being used to display the list's data

 5     SimpleCursorAdapter mAdapter;

 6 

 7     // These are the Contacts rows that we will retrieve

 8     static final String[] PROJECTION = new String[] {ContactsContract.Data._ID,

 9             ContactsContract.Data.DISPLAY_NAME};

10 

11     // This is the select criteria

12     static final String SELECTION = "((" + 

13             ContactsContract.Data.DISPLAY_NAME + " NOTNULL) AND (" +

14             ContactsContract.Data.DISPLAY_NAME + " != '' ))";

15 

16     @Override

17     protected void onCreate(Bundle savedInstanceState) {

18         super.onCreate(savedInstanceState);

19 

20         // Create a progress bar to display while the list loads

21         ProgressBar progressBar = new ProgressBar(this);

22         progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,

23                 LayoutParams.WRAP_CONTENT, Gravity.CENTER));

24         progressBar.setIndeterminate(true);

25         getListView().setEmptyView(progressBar);

26 

27         // Must add the progress bar to the root of the layout

28         ViewGroup root = (ViewGroup) findViewById(android.R.id.content);

29         root.addView(progressBar);

30 

31         // For the cursor adapter, specify which columns go into which views

32         String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};

33         int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1

34 

35         // Create an empty adapter we will use to display the loaded data.

36         // We pass null for the cursor, then update it in onLoadFinished()

37         mAdapter = new SimpleCursorAdapter(this, 

38                 android.R.layout.simple_list_item_1, null,

39                 fromColumns, toViews, 0);

40         setListAdapter(mAdapter);

41 

42         // Prepare the loader.  Either re-connect with an existing one,

43         // or start a new one.

44         getLoaderManager().initLoader(0, null, this);

45     }

46 

47     // Called when a new Loader needs to be created

48     public Loader<Cursor> onCreateLoader(int id, Bundle args) {

49         // Now create and return a CursorLoader that will take care of

50         // creating a Cursor for the data being displayed.

51         return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,

52                 PROJECTION, SELECTION, null, null);

53     }

54 

55     // Called when a previously created loader has finished loading

56     public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

57         // Swap the new cursor in.  (The framework will take care of closing the

58         // old cursor once we return.)

59         mAdapter.swapCursor(data);

60     }

61 

62     // Called when a previously created loader is reset, making the data unavailable

63     public void onLoaderReset(Loader<Cursor> loader) {

64         // This is called when the last Cursor provided to onLoadFinished()

65         // above is about to be closed.  We need to make sure we are no

66         // longer using it.

67         mAdapter.swapCursor(null);

68     }

69 

70     @Override 

71     public void onListItemClick(ListView l, View v, int position, long id) {

72         // Do something when a list item is clicked

73     }

74 }

 

你可能感兴趣的:(Cursor)