android多选联系人实现

可结合此方案更有效

  1. publicclassCopyContactsListMultipleextendsListActivityimplementsOnClickListener{
  2. privatefinalintUPDATE_LIST=1;
  3. ArrayList<String>contactsList;//得到的所有联系人
  4. ArrayList<String>getcontactsList;//选择得到联系人
  5. privateButtonokbtn;
  6. privateButtoncancelbtn;
  7. privateProgressDialogproDialog;
  8. Threadgetcontacts;
  9. HandlerupdateListHandler=newHandler(){
  10. publicvoidhandleMessage(Messagemsg){
  11. switch(msg.what){
  12. caseUPDATE_LIST:
  13. if(proDialog!=null){
  14. proDialog.dismiss();
  15. }
  16. updateList();
  17. }
  18. }
  19. };
  20. publicvoidonCreate(BundlesavedInstanceState){
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.contactslist);
  23. contactsList=newArrayList<String>();
  24. getcontactsList=newArrayList<String>();
  25. finalListViewlistView=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=newThread(newGetContacts());
  33. getcontacts.start();
  34. proDialog=ProgressDialog.show(CopyContactsListMultiple.this,"loading","loading",true,true);
  35. }
  36. @Override
  37. protectedvoidonResume(){
  38. //TODOAuto-generatedmethodstub
  39. super.onResume();
  40. }
  41. voidupdateList(){
  42. if(contactsList!=null)
  43. setListAdapter(newArrayAdapter<String>(this,
  44. android.R.layout.simple_list_item_multiple_choice,contactsList));
  45. }
  46. @Override
  47. protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){
  48. //TODOAuto-generatedmethodstub
  49. if(!((CheckedTextView)v).isChecked()){
  50. CharSequencenum=((CheckedTextView)v).getText();
  51. getcontactsList.add(num.toString());
  52. }
  53. if(((CheckedTextView)v).isChecked()){
  54. CharSequencenum=((CheckedTextView)v).getText();
  55. if((num.toString()).indexOf("[")>0){
  56. StringphoneNum=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. classGetContactsimplementsRunnable{
  67. @Override
  68. publicvoidrun(){
  69. //TODOAuto-generatedmethodstub
  70. Uriuri=ContactsContract.Contacts.CONTENT_URI;
  71. String[]projection=newString[]{
  72. ContactsContract.Contacts._ID,
  73. ContactsContract.Contacts.DISPLAY_NAME,
  74. ContactsContract.Contacts.PHOTO_ID
  75. };
  76. Stringselection=ContactsContract.Contacts.IN_VISIBLE_GROUP+"='1'";
  77. String[]selectionArgs=null;
  78. StringsortOrder=ContactsContract.Contacts.DISPLAY_NAME+"COLLATELOCALIZEDASC";
  79. Cursorcursor=managedQuery(uri,projection,selection,selectionArgs,sortOrder);
  80. Cursorphonecur=null;
  81. while(cursor.moveToNext()){
  82. //取得联系人名字
  83. intnameFieldColumnIndex=cursor.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);
  84. Stringname=cursor.getString(nameFieldColumnIndex);
  85. //取得联系人ID
  86. StringcontactId=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. StringstrPhoneNumber=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. Messagemsg1=newMessage();
  100. msg1.what=UPDATE_LIST;
  101. updateListHandler.sendMessage(msg1);
  102. }
  103. }
  104. @Override
  105. protectedvoidonPause(){
  106. //TODOAuto-generatedmethodstub
  107. super.onPause();
  108. }
  109. @Override
  110. protectedvoidonDestroy(){
  111. contactsList.clear();
  112. getcontactsList.clear();
  113. super.onDestroy();
  114. }
  115. @Override
  116. publicvoidonClick(Viewv){
  117. //TODOAuto-generatedmethodstub
  118. switch(v.getId()){
  119. caseR.id.contacts_done_button:
  120. Intenti=newIntent();
  121. if(getcontactsList!=null&&getcontactsList.size()>0){
  122. Bundleb=newBundle();
  123. b.putStringArrayList("GET_CONTACT",getcontactsList);
  124. i.putExtras(b);
  125. }
  126. setResult(RESULT_OK,i);
  127. CopyContactsListMultiple.this.finish();
  128. break;
  129. caseR.id.contact_back_button:
  130. CopyContactsListMultiple.this.finish();
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. @Override
  137. publicbooleanonKeyDown(intkeyCode,KeyEventevent){
  138. //TODOAuto-generatedmethodstub
  139. if(keyCode==KeyEvent.KEYCODE_BACK){
  140. Intenti=newIntent();
  141. Bundleb=newBundle();
  142. b.putStringArrayList("GET_CONTACT",getcontactsList);
  143. i.putExtras(b);//}
  144. setResult(RESULT_OK,i);
  145. }
  146. returnsuper.onKeyDown(keyCode,event);
  147. }
  148. }

以上代码若有错误,请试用以下代码

class GetContacts implements Runnable {
@Override
public void run() {
Cursor cursor = null;
try {

Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
cursor=managedQuery(uri, projection, selection, selectionArgs, sortOrder);
LogPrint.Print("lock", "cursor.getCount()======"+cursor.getCount());
if (cursor.getCount() > 0) {
while (cursor.moveToNext()){
// 取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);
String name = cursor.getString(nameFieldColumnIndex);
// 取得联系人ID
String contactId = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));
Cursor phonecur = managedQuery(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
// 取得电话号码(可能存在多个号码)
LogPrint.Print("lock", "phonecur.getCount()======"+phonecur.getCount());
if (phonecur.getCount() > 0) {

while (phonecur.moveToNext()){
String strPhoneNumber = phonecur.getString(phonecur.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));
if(strPhoneNumber.length()>4)

person = new Person();
person.setName(name);
person.setPhone(strPhoneNumber);
contactsList.add(person);

}

if (phonecur != null){

phonecur.close();

}
}
}

Message msg1 = new Message();
msg1.what = UPDATE_LIST;
updateListHandler.sendMessage(msg1);
}else{
//未找到任何联系人

Message msg1 = new Message();
msg1.what = 1001;
updateListHandler.sendMessage(msg1);
}
} catch (Exception e) {

e.printStackTrace();
}finally{
if (cursor != null) {

cursor.close();

}
}
}
}

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <ListViewandroid: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. <LinearLayoutandroid: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. <Buttonandroid: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. <Buttonandroid: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>

更多相关文章

  1. Android利用WebService查询手机号码归属地
  2. Android中textView自动识别电话号码,电子邮件,网址(自动加连接)
  3. android获取手机号码以及imsi信息
  4. Android 学习笔记 Contacts (三)Contacts 查找,增加,更新,删除联系人
  5. Android联系人数据库全解析(2)
  6. android 通过广播获取指定联系人短信内容
  7. 转:Android联系人数据库全解析
  8. Android基于PinnedSectionListView实现联系人通讯录

随机推荐

  1. Android(安卓)ListView里设置默认Item的
  2. Android(安卓)ImageView图片自适应
  3. JS判断客户端是否是iOS或者Android手机移
  4. Android获取ROOT权限
  5. Android调用js的坑
  6. Android搜索建议(搜索联想)
  7. Android(安卓)7.0 移除设置中的某些项(辅
  8. Android(安卓)--- 图片的特效处理
  9. Android(安卓)经典小技巧总结
  10. 第15天android:使用sqlite