android 多选联系人

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

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

  1. public class CopyContactsListMultiple extends ListActivity implements OnClickListener{
  2. private final int UPDATE_LIST=1;
  3. ArrayList contactsList; //得到的所有联系人
  4. ArrayList getcontactsList; //选择得到联系人
  5. private Button okbtn;
  6. private Button cancelbtn;
  7. private ProgressDialog proDialog;
  8. Thread getcontacts;
  9. Handler updateListHandler = new Handler() {
  10. public void handleMessage(Message msg) {
  11. switch (msg.what) {
  12. case UPDATE_LIST:
  13. if (proDialog != null) {
  14. proDialog.dismiss();
  15. }
  16. updateList();
  17. }
  18. }
  19. };
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.contactslist);
  23. contactsList=new ArrayList();
  24. getcontactsList=new ArrayList();
  25. final ListView listView = getListView();
  26. listView.setItemsCanFocus(false);
  27. listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  28. okbtn=(Button)findViewById(R.id.contacts_done_button);
  29. cancelbtn=(Button)findViewById(R.id.contact_back_button);
  30. okbtn.setOnClickListener(this);
  31. cancelbtn.setOnClickListener(this);
  32. getcontacts=new Thread(new GetContacts());
  33. getcontacts.start();
  34. proDialog = ProgressDialog.show(CopyContactsListMultiple.this, “loading”,“loading”, true, true);
  35. }
  36. @Override
  37. protected void onResume() {
  38. // TODO Auto-generated method stub
  39. super.onResume();
  40. }
  41. void updateList(){
  42. if(contactsList!=null)
  43. setListAdapter(new ArrayAdapter(this,
  44. android.R.layout.simple_list_item_multiple_choice, contactsList));
  45. }
  46. @Override
  47. protected void onListItemClick(ListView l, View v, int position, long id) {
  48. // TODO Auto-generated method stub
  49. if(!((CheckedTextView)v).isChecked()){
  50. CharSequence num=((CheckedTextView)v).getText();
  51. getcontactsList.add(num.toString());
  52. }
  53. if(((CheckedTextView)v).isChecked()){
  54. CharSequence num=((CheckedTextView)v).getText();
  55. if((num.toString()).indexOf(“[")>0){
  56. String phoneNum=num.toString().substring(0, (num.toString()).indexOf("\n"));
  57. getcontactsList.remove(phoneNum);
  58. Log.d("remove_num", ""+phoneNum);
  59. }else{
  60. getcontactsList.remove(num.toString());
  61. Log.d("remove_num", ""+num.toString());
  62. }
  63. }
  64. super.onListItemClick(l, v, position, id);
  65. }
  66. class GetContacts implements Runnable{
  67. @Override
  68. public void run() {
  69. // TODO Auto-generated method stub
  70. Uri uri = ContactsContract.Contacts.CONTENT_URI;
  71. String[] projection = new String[] {
  72. ContactsContract.Contacts._ID,
  73. ContactsContract.Contacts.DISPLAY_NAME,
  74. ContactsContract.Contacts.PHOTO_ID
  75. };
  76. String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + “ = ’1′”;
  77. String[] selectionArgs = null;
  78. String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + “ COLLATE LOCALIZED ASC”;
  79. Cursor cursor=managedQuery(uri, projection, selection, selectionArgs, sortOrder);
  80. Cursor phonecur = null;
  81. while (cursor.moveToNext()){
  82. // 取得联系人名字
  83. int nameFieldColumnIndex = cursor.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);
  84. String name = cursor.getString(nameFieldColumnIndex);
  85. // 取得联系人ID
  86. String contactId = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));
  87. phonecur = managedQuery(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID + “ = ” + contactId, null, null);
  88. // 取得电话号码(可能存在多个号码)
  89. while (phonecur.moveToNext()){
  90. String strPhoneNumber = phonecur.getString(phonecur.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));
  91. if(strPhoneNumber.length()>4)
  92. contactsList.add(“18610011001″+“\n测试”);
  93. //contactsList.add(strPhoneNumber+”\n”+name+”");
  94. }
  95. }
  96. if(phonecur!=null)
  97. phonecur.close();
  98. cursor.close();
  99. Message msg1=new Message();
  100. msg1.what=UPDATE_LIST;
  101. updateListHandler.sendMessage(msg1);
  102. }
  103. }
  104. @Override
  105. protected void onPause() {
  106. // TODO Auto-generated method stub
  107. super.onPause();
  108. }
  109. @Override
  110. protected void onDestroy() {
  111. contactsList.clear();
  112. getcontactsList.clear();
  113. super.onDestroy();
  114. }
  115. @Override
  116. public void onClick(View v) {
  117. // TODO Auto-generated method stub
  118. switch (v.getId()) {
  119. case R.id.contacts_done_button:
  120. Intent i = new Intent();
  121. if(getcontactsList!=null>>getcontactsList.size()>0){
  122. Bundle b = new Bundle();
  123. b.putStringArrayList(“GET_CONTACT”, getcontactsList);
  124. i.putExtras(b);
  125. }
  126. setResult(RESULT_OK, i);
  127. CopyContactsListMultiple.this.finish();
  128. break;
  129. case R.id.contact_back_button:
  130. CopyContactsListMultiple.this.finish();
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. @Override
  137. public boolean onKeyDown(int keyCode, KeyEvent event) {
  138. // TODO Auto-generated method stub
  139. if(keyCode==KeyEvent.KEYCODE_BACK){
  140. Intent i = new Intent();
  141. Bundle b = new Bundle();
  142. b.putStringArrayList(“GET_CONTACT”, getcontactsList);
  143. i.putExtras(b); // }
  144. setResult(RESULT_OK, i);
  145. }
  146. return super.onKeyDown(keyCode, event);
  147. }
  148. }

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. <ListView android:id=“@+id/android:list”
  6. android:layout_height=“fill_parent”
  7. android:layout_width=“fill_parent”
  8. android:layout_marginLeft=“10dip”
  9. android:layout_marginRight=“10dip”
  10. android:layout_marginTop=“10dip”
  11. android:layout_weight=“1.0″>
  12. </ListView>
  13. <LinearLayout android:layout_width=“fill_parent”
  14. android:layout_height=“wrap_content”
  15. android:layout_weight=“0″ android:orientation=“horizontal”
  16. android:gravity=“center” android:layout_marginLeft=“10dip”
  17. android:layout_marginRight=“10dip” android:layout_marginBottom=“10dip”
  18. android:weightSum=“1″>
  19. <Button android:id=“@+id/contacts_done_button”
  20. android:textSize=“17dip”
  21. android:layout_marginRight=“10dip” android:layout_width=“0dip”
  22. android:layout_height=“wrap_content” android:layout_weight=“0.35″
  23. android:text=“sure” />
  24. <Button android:id=“@+id/contact_back_button”
  25. android:layout_marginLeft=“10dip” android:textSize=“17dip”
  26. android:layout_width=“0dip” android:layout_height=“wrap_content”
  27. android:layout_weight=“0.35″ android:text=“back” />
  28. </LinearLayout>
  29. </LinearLayout>

效果如图:

android 多选联系人

上一篇:安全将成为网络经济的下一个风口


下一篇:专家在汉热议互联网威胁 网络安全人才缺口近百万