Android实现多选联系人

移动开发 Android
有很多网友问多选联系人实现方式,这里参考了apidemos的例子做了简单实现。整体思路是使用使用一个ArrayList存放选中的联系人信息,细节就不说了,贴一下代码

有很多网友问多选联系人实现方式,这里参考了apidemos的例子做了简单实现。

整体思路是使用使用一个ArrayList存放选中的联系人信息,细节就不说了,贴一下代码

  1. public class CopyContactsListMultiple extends ListActivity implements OnClickListener{  
  2.    
  3. private final int UPDATE_LIST=1;  
  4. ArrayList contactsList; //得到的所有联系人  
  5. ArrayList getcontactsList; //选择得到联系人  
  6. private Button okbtn;  
  7. private Button cancelbtn;  
  8. private ProgressDialog proDialog;  
  9.    
  10. Thread getcontacts;  
  11. Handler updateListHandler = new Handler() {  
  12. public void handleMessage(Message msg) {  
  13. switch (msg.what) {  
  14.    
  15. case UPDATE_LIST:  
  16. if (proDialog != null) {  
  17. proDialog.dismiss();  
  18. }  
  19. updateList();  
  20. }  
  21. }  
  22. };  
  23. public void onCreate(Bundle savedInstanceState) {  
  24. super.onCreate(savedInstanceState);  
  25. setContentView(R.layout.contactslist);  
  26. contactsList=new ArrayList();  
  27. getcontactsList=new ArrayList();  
  28.    
  29. final ListView listView = getListView();  
  30. listView.setItemsCanFocus(false);  
  31. listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);  
  32. okbtn=(Button)findViewById(R.id.contacts_done_button);  
  33. cancelbtn=(Button)findViewById(R.id.contact_back_button);  
  34. okbtn.setOnClickListener(this);  
  35. cancelbtn.setOnClickListener(this);  
  36.    
  37. getcontacts=new Thread(new GetContacts());  
  38. getcontacts.start();  
  39. proDialog = ProgressDialog.show(CopyContactsListMultiple.this, “loading”,“loading”, truetrue);  
  40.    
  41. }  
  42.    
  43. @Override 
  44. protected void onResume() {  
  45. // TODO Auto-generated method stub  
  46. super.onResume();  
  47.    
  48. }  
  49.    
  50. void updateList(){  
  51. if(contactsList!=null)  
  52. setListAdapter(new ArrayAdapter(this,  
  53. android.R.layout.simple_list_item_multiple_choice, contactsList));  
  54.    
  55. }  
  56.    
  57. @Override 
  58. protected void onListItemClick(ListView l, View v, int position, long id) {  
  59. // TODO Auto-generated method stub  
  60. if(!((CheckedTextView)v).isChecked()){  
  61.    
  62. CharSequence num=((CheckedTextView)v).getText();  
  63. getcontactsList.add(num.toString());  
  64. }  
  65. if(((CheckedTextView)v).isChecked()){  
  66. CharSequence num=((CheckedTextView)v).getText();  
  67. if((num.toString()).indexOf(“[")>0){  
  68. String phoneNum=num.toString().substring(0, (num.toString()).indexOf("\n"));  
  69. getcontactsList.remove(phoneNum);  
  70. Log.d("remove_num"""+phoneNum);  
  71. }else{  
  72. getcontactsList.remove(num.toString());  
  73. Log.d("remove_num"""+num.toString());  
  74. }  
  75. }  
  76. super.onListItemClick(l, v, position, id);  
  77. }  
  78. class GetContacts implements Runnable{  
  79. @Override 
  80. public void run() {  
  81. // TODO Auto-generated method stub  
  82. Uri uri = ContactsContract.Contacts.CONTENT_URI;  
  83. String[] projection = new String[] {  
  84. ContactsContract.Contacts._ID,  
  85. ContactsContract.Contacts.DISPLAY_NAME,  
  86. ContactsContract.Contacts.PHOTO_ID  
  87. };  
  88. String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + “ = ’1′”;  
  89. String[] selectionArgs = null;  
  90. String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + “ COLLATE LOCALIZED ASC”;  
  91. Cursor cursor=managedQuery(uri, projection, selection, selectionArgs, sortOrder);  
  92. Cursor phonecur = null;  
  93.    
  94. while (cursor.moveToNext()){  
  95.    
  96. // 取得联系人名字  
  97. int nameFieldColumnIndex = cursor.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);  
  98. String name = cursor.getString(nameFieldColumnIndex);  
  99. // 取得联系人ID 

 

  1. String contactId = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));  
  2. phonecur = managedQuery(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID + “ = ” + contactId, null, null);  
  3. // 取得电话号码(可能存在多个号码)  
  4. while (phonecur.moveToNext()){  
  5. String strPhoneNumber = phonecur.getString(phonecur.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));  
  6. if(strPhoneNumber.length()>4)  
  7. contactsList.add(“18610011001″+“\n测试”);  
  8. //contactsList.add(strPhoneNumber+”\n”+name+”");  
  9.    
  10. }  
  11. }  
  12. if(phonecur!=null)  
  13. phonecur.close();  
  14. cursor.close();  
  15.    
  16. Message msg1=new Message();  
  17. msg1.what=UPDATE_LIST;  
  18. updateListHandler.sendMessage(msg1);  
  19. }  
  20. }  
  21. @Override  
  22. protected void onPause() {  
  23. // TODO Auto-generated method stub  
  24. super.onPause();  
  25.    
  26. }  
  27.    
  28. @Override  
  29. protected void onDestroy() {  
  30. contactsList.clear();  
  31. getcontactsList.clear();  
  32. super.onDestroy();  
  33. }  
  34.    
  35. @Override  
  36. public void onClick(View v) {  
  37. // TODO Auto-generated method stub  
  38. switch (v.getId()) {  
  39. case R.id.contacts_done_button:  
  40. Intent i = new Intent();  
  41. if(getcontactsList!=null>>getcontactsList.size()>0){  
  42. Bundle b = new Bundle();  
  43. b.putStringArrayList(“GET_CONTACT”, getcontactsList);  
  44. i.putExtras(b);  
  45. }  
  46. setResult(RESULT_OK, i);  
  47. CopyContactsListMultiple.this.finish();  
  48. break;  
  49. case R.id.contact_back_button:  
  50. CopyContactsListMultiple.this.finish();  
  51. break;  
  52. default:  
  53. break;  
  54. }  
  55. }  
  56. @Override  
  57. public boolean onKeyDown(int keyCode, KeyEvent event) {  
  58. // TODO Auto-generated method stub  
  59. if(keyCode==KeyEvent.KEYCODE_BACK){  
  60. Intent i = new Intent();  
  61. Bundle b = new Bundle();  
  62. b.putStringArrayList(“GET_CONTACT”, getcontactsList);  
  63. i.putExtras(b); // }  
  64. setResult(RESULT_OK, i);  
  65. }  
  66. return super.onKeyDown(keyCode, event);  
  67. }  

xml:

  1. <?xml version=“1.0″ encoding=“utf-8″?> 
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.         android:orientation=“vertical” android:layout_width=“fill_parent”  
  4.         android:layout_height=“fill_parent”> 
  5.    
  6.    
  7.         <ListView android:id=“@+id/android:list”   
  8.                 android:layout_height=“fill_parent”   
  9.                 android:layout_width=“fill_parent”  
  10.                  android:layout_marginLeft=“10dip”  
  11.                 android:layout_marginRight=“10dip”   
  12.                 android:layout_marginTop=“10dip”  
  13.                 android:layout_weight=“1.0″> 
  14.         </ListView> 
  15.    
  16.         <LinearLayout android:layout_width=“fill_parent”  
  17.                 android:layout_height=“wrap_content”  
  18.                 android:layout_weight=“0″ android:orientation=“horizontal”  
  19.                 android:gravity=“center” android:layout_marginLeft=“10dip”  
  20.                 android:layout_marginRight=“10dip” android:layout_marginBottom=“10dip”  
  21.                 android:weightSum=“1″> 
  22.    
  23.                 <Button android:id=“@+id/contacts_done_button”  
  24.                         android:textSize=“17dip”  
  25.                         android:layout_marginRight=“10dip” android:layout_width=“0dip”  
  26.                         android:layout_height=“wrap_content” android:layout_weight=“0.35″  
  27.                         android:text=“sure” /> 
  28.    
  29.                 <Button android:id=“@+id/contact_back_button”  
  30.                         android:layout_marginLeft=“10dip” android:textSize=“17dip”  
  31.                         android:layout_width=“0dip” android:layout_height=“wrap_content”  
  32.                         android:layout_weight=“0.35″ android:text=“back” /> 
  33.         </LinearLayout> 
  34.    
  35. </LinearLayout> 

效果如图:

 

device-2012-02-06-091606.png

【编辑推荐】

  1. Android编程方法大PK:NDK vs. RenderScript 
  2. Android SQLite3命令详解教程 
  3. 如何开发基于Adobe AIR的Android应用 
责任编辑:冰凝儿 来源: DEVDIV博客
相关推荐

2014-12-30 11:51:35

ListViewItem View

2011-05-26 14:42:34

Android 手机

2010-01-27 14:08:56

Android查询联系

2015-01-21 15:50:55

Android源码全国城市列表

2015-11-11 10:17:15

ios9联系人框架干货

2020-02-02 14:45:55

联系人开源工具

2011-08-12 10:16:10

iPhone通讯录联系人

2013-09-17 09:51:49

谷歌Bump移动应用

2012-03-26 21:38:36

智能

2012-02-24 09:25:58

2011-09-21 14:33:17

点心

2019-11-07 09:20:36

Windows 10联系人Outlook

2013-05-07 09:26:26

Office 365微软

2015-09-24 11:37:43

2011-10-14 09:42:06

点心通讯录

2012-02-02 17:16:11

Windows PhoC#联系人资料

2020-04-29 09:55:13

苹果谷歌API

2017-08-25 09:50:58

微信重度用户优化

2010-11-23 11:21:25

Microsoft L

2014-12-10 10:45:56

Android应用权限
点赞
收藏

51CTO技术栈公众号