Android实现多选联系人

整体思路是使用使用一个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> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值