其中Activity实现了 LoaderCallbacks接口,为的是可以用
CursorLoader动态的加载数据给ListView显示.
1 import android.app.ListActivity; 2 import android.app.LoaderManager; 3 import android.content.CursorLoader; 4 import android.content.Loader; 5 import android.database.Cursor; 6 import android.os.Bundle; 7 import android.provider.ContactsContract; 8 import android.view.Gravity; 9 import android.view.Menu; 10 import android.view.View; 11 import android.view.ViewGroup; 12 import android.view.ViewGroup.LayoutParams; 13 import android.widget.FrameLayout; 14 import android.widget.ListView; 15 import android.widget.ProgressBar; 16 import android.widget.SimpleCursorAdapter; 17 import android.widget.TextView; 18 import android.widget.Toast; 19 20 public class ListViewLoader extends ListActivity implements 21 LoaderManager.LoaderCallbacks<Cursor> { 22 // This is the Adapter being used to display the list's data 23 SimpleCursorAdapter mAdapter; 24 // These are the Contacts rows that we will retrieve 25 static final String[] PROJECTION = { ContactsContract.Data._ID, 26 ContactsContract.Data.DISPLAY_NAME }; 27 // This is the select criteria 28 static final String SELECTION = "((" + ContactsContract.Data.DISPLAY_NAME 29 + " NOT NULL) AND (" + ContactsContract.Data.DISPLAY_NAME 30 + "!=''))"; 31 32 @Override 33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 // Create a progress bar to display while the list loads 36 ProgressBar mProgressBar = new ProgressBar(this); 37 mProgressBar.setLayoutParams(new FrameLayout.LayoutParams( 38 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 39 Gravity.CENTER)); 40 mProgressBar.setIndeterminate(true); 41 getListView().setEmptyView(mProgressBar); 42 43 // Must add the progress bar to the root of the layout 44 ViewGroup root = (ViewGroup) findViewById(android.R.id.content); 45 root.addView(mProgressBar); 46 47 // For the cursor adapter, specify which columns go into which views 48 String[] fromColumns = { ContactsContract.Data.DISPLAY_NAME }; 49 int[] toViews = { android.R.id.text1 }; 50 51 // Create an empty adapter we will use to display the loaded data. 52 // We pass null for the cursor, then update it in onLoadFinished() 53 mAdapter = new SimpleCursorAdapter(this, 54 android.R.layout.simple_list_item_1, null, fromColumns, 55 toViews, 0); 56 setListAdapter(mAdapter); 57 58 // Prepare the loader. Either re-connect with an existing one, 59 // or start a new one. 60 getLoaderManager().initLoader(0, null, this); 61 62 } 63 64 @Override 65 public boolean onCreateOptionsMenu(Menu menu) { 66 // Inflate the menu; this adds items to the action bar if it is present. 67 getMenuInflater().inflate(R.menu.list_view_loader, menu); 68 return true; 69 } 70 71 // Called when a new Loader needs to be created 72 @Override 73 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 74 // Now create and return a CursorLoader that will take care of 75 // creating a Cursor for the data being displayed. 76 return new CursorLoader(this, ContactsContract.Data.CONTENT_URI, 77 PROJECTION, SELECTION, null, null); 78 } 79 80 // Called when a previously created loader has finished loading 81 @Override 82 public void onLoadFinished(Loader<Cursor> arg0, Cursor c) { 83 // Swap the new cursor in. (The framework will take care of closing the 84 // old cursor once we return.) 85 mAdapter.swapCursor(c); 86 87 } 88 89 // Called when a previously created loader is reset, making the data 90 // unavailable 91 @Override 92 public void onLoaderReset(Loader<Cursor> arg0) { 93 // This is called when the last Cursor provided to onLoadFinished() 94 // above is about to be closed. We need to make sure we are no 95 // longer using it. 96 mAdapter.swapCursor(null); 97 } 98 99 @Override 100 protected void onListItemClick(ListView l, View v, int position, long id) { 101 String text = ((TextView) v).getText().toString(); 102 Toast.makeText(this, "点击了:" + text, Toast.LENGTH_SHORT).show(); 103 } 104 105 }
查询手机联系人用到权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
用到的API支持系统版本:
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" />