通讯录:管理联系人
1.修改数据
2.访问数据
3.将数据放入适配器并显示
需要权限:
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
布局文件:TextView
在模拟器通讯录中创建一个联系人:
-------------------------------------------------------
1 public class MainActivity extends ActionBarActivity implements 2 LoaderCallbacks<Cursor> { 3 private TextView textView; 4 private LoaderManager manager; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.fragment_main); 10 textView = (TextView) findViewById(R.id.mag); 11 manager = getLoaderManager(); 12 manager.initLoader(1001, null, this);// 初始化 13 14 } 15 16 @Override 17 public boolean onCreateOptionsMenu(Menu menu) { 18 19 // Inflate the menu; this adds items to the action bar if it is present. 20 getMenuInflater().inflate(R.menu.main, menu); 21 return true; 22 } 23 24 @Override 25 public boolean onOptionsItemSelected(MenuItem item) { 26 // Handle action bar item clicks here. The action bar will 27 // automatically handle clicks on the Home/Up button, so long 28 // as you specify a parent activity in AndroidManifest.xml. 29 int id = item.getItemId(); 30 if (id == R.id.action_settings) { 31 return true; 32 } 33 return super.onOptionsItemSelected(item); 34 } 35 36 /** 37 * A placeholder fragment containing a simple view. 38 */ 39 public static class PlaceholderFragment extends Fragment { 40 41 public PlaceholderFragment() { 42 } 43 44 @Override 45 public View onCreateView(LayoutInflater inflater, ViewGroup container, 46 Bundle savedInstanceState) { 47 View rootView = inflater.inflate(R.layout.fragment_main, container, 48 false); 49 return rootView; 50 } 51 } 52 53 @Override 54 public Loader<Cursor> onCreateLoader(int id, Bundle args) { 55 // TODO Auto-generated method stub 56 Uri uri = ContactsContract.Contacts.CONTENT_URI; 57 CursorLoader loader = new CursorLoader(this, uri, null, null, null, 58 null); 59 60 return loader; 61 } 62 63 @Override 64 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 65 // TODO Auto-generated method stub 66 StringBuffer buffer = new StringBuffer(); 67 while (data.moveToNext()) { 68 int nameindex = data 69 .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 70 String dispaly_name = data.getString(nameindex); 71 buffer.append("name: " + dispaly_name+" Tel :"); 72 // 获得通讯录每一条记录的ID 73 int _id = data.getInt(data 74 .getColumnIndex(ContactsContract.Contacts._ID)); 75 Cursor cursor = getContentResolver().query( 76 ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 77 null, 78 ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" 79 + _id, null, null); 80 while(cursor.moveToNext()){ 81 String phone=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 82 buffer.append(phone); 83 } 84 85 } 86 textView.setText(buffer.toString()); 87 } 88 89 @Override 90 public void onLoaderReset(Loader<Cursor> loader) { 91 // TODO Auto-generated method stub 92 93 } 94 95 }
---