`

android多选联系人实现

阅读更多

 

android多选联系人实现

 

 

可结合此方案更有效

  1. public class CopyContactsListMultiple extends ListActivity implements OnClickListener{  
  2.       
  3.     private final int UPDATE_LIST=1;  
  4.     ArrayList<String> contactsList; //得到的所有联系人  
  5.     ArrayList<String> 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<String>();  
  27.         getcontactsList=new ArrayList<String>();  
  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.   
  51.     void updateList(){  
  52.         if(contactsList!=null)  
  53.         setListAdapter(new ArrayAdapter<String>(this,  
  54.                android.R.layout.simple_list_item_multiple_choice, contactsList));  
  55.           
  56.     }  
  57.   
  58.       
  59.     @Override  
  60.     protected void onListItemClick(ListView l, View v, int position, long id) {  
  61.         // TODO Auto-generated method stub  
  62.         if(!((CheckedTextView)v).isChecked()){  
  63.               
  64.             CharSequence num=((CheckedTextView)v).getText();  
  65.             getcontactsList.add(num.toString());  
  66.         }  
  67.             if(((CheckedTextView)v).isChecked()){     
  68.                 CharSequence num=((CheckedTextView)v).getText();  
  69.                 if((num.toString()).indexOf("[")>0){  
  70.                     String phoneNum=num.toString().substring(0, (num.toString()).indexOf("\n"));  
  71.                     getcontactsList.remove(phoneNum);  
  72.                     Log.d("remove_num"""+phoneNum);  
  73.                     }else{  
  74.                          getcontactsList.remove(num.toString());  
  75.                         Log.d("remove_num"""+num.toString());  
  76.                     }  
  77.         }  
  78.         super.onListItemClick(l, v, position, id);    
  79.     }  
  80.    class GetContacts implements Runnable{  
  81.        @Override  
  82.     public void run() {  
  83.         // TODO Auto-generated method stub  
  84.            Uri uri = ContactsContract.Contacts.CONTENT_URI;  
  85.            String[] projection = new String[] {  
  86.                     ContactsContract.Contacts._ID,  
  87.                     ContactsContract.Contacts.DISPLAY_NAME,  
  88.                     ContactsContract.Contacts.PHOTO_ID  
  89.             };  
  90.             String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";  
  91.             String[] selectionArgs = null;  
  92.             String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";  
  93.             Cursor cursor=managedQuery(uri, projection, selection, selectionArgs, sortOrder);  
  94.             Cursor phonecur = null;  
  95.   
  96.             while (cursor.moveToNext()){    
  97.                   
  98.                 // 取得联系人名字    
  99.                 int nameFieldColumnIndex = cursor.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);    
  100.                 String name = cursor.getString(nameFieldColumnIndex);    
  101.                 // 取得联系人ID    
  102.                 String contactId = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));    
  103.                 phonecur = managedQuery(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "  + contactId, nullnull);     
  104.                 // 取得电话号码(可能存在多个号码)    
  105.                 while (phonecur.moveToNext()){    
  106.                   String strPhoneNumber = phonecur.getString(phonecur.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));    
  107.                     if(strPhoneNumber.length()>4)  
  108.                         contactsList.add("18610011001"+"\n测试");  
  109.                     //contactsList.add(strPhoneNumber+"\n"+name+"");  
  110.               
  111.                 }      
  112.             }    
  113.             if(phonecur!=null)  
  114.              phonecur.close();  
  115.             cursor.close();   
  116.   
  117.             Message msg1=new Message();  
  118.             msg1.what=UPDATE_LIST;  
  119.             updateListHandler.sendMessage(msg1);  
  120.     }   
  121.    }  
  122. @Override  
  123. protected void onPause() {  
  124.     // TODO Auto-generated method stub  
  125.     super.onPause();  
  126.       
  127. }  
  128.   
  129. @Override  
  130. protected void onDestroy() {  
  131.     contactsList.clear();  
  132.     getcontactsList.clear();  
  133.     super.onDestroy();  
  134. }  
  135.   
  136.   
  137. @Override  
  138. public void onClick(View v) {  
  139.     // TODO Auto-generated method stub  
  140.     switch (v.getId()) {  
  141.     case R.id.contacts_done_button:  
  142.            Intent i = new Intent();   
  143.            if(getcontactsList!=null&&getcontactsList.size()>0){  
  144.                Bundle b = new Bundle();    
  145.                b.putStringArrayList("GET_CONTACT", getcontactsList);  
  146.                i.putExtras(b);    
  147.             }  
  148.             setResult(RESULT_OK, i);    
  149.             CopyContactsListMultiple.this.finish();  
  150.         break;  
  151.     case R.id.contact_back_button:  
  152.         CopyContactsListMultiple.this.finish();  
  153.         break;  
  154.     default:  
  155.         break;  
  156.     }  
  157. }  
  158. @Override  
  159. public boolean onKeyDown(int keyCode, KeyEvent event) {  
  160.     // TODO Auto-generated method stub  
  161.     if(keyCode==KeyEvent.KEYCODE_BACK){  
  162.            Intent i = new Intent();   
  163.             Bundle b = new Bundle();    
  164.             b.putStringArrayList("GET_CONTACT", getcontactsList);  
  165.             i.putExtras(b); // }  
  166.             setResult(RESULT_OK, i);    
  167.     }  
  168.     return super.onKeyDown(keyCode, event);  
  169. }  
  170. }  

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

 

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();
     
    }
   }
  }
 }
 

  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>  
分享到:
评论

相关推荐

    Android调用通讯录 查询多选联系人 自动提示补全 发送信息

    在Android平台上,开发一款应用需要调用通讯录、支持多选联系人以及提供查询时的自动提示补全功能,并能够发送信息,这涉及到多个关键知识点。以下是对这些知识点的详细说明: 1. **调用通讯录**:Android系统提供...

    android中listview多选demo

    "android中listview多选demo"就是一个展示如何实现这一功能的示例项目。 在Android的ListView中实现多选,通常涉及到以下几个关键知识点: 1. **CheckedTextView**:这是Android提供的一个内置视图,它继承自...

    android list View多选

    ListView的多选功能是用户交互中常见的需求,特别是在需要用户选择多个项目时,如设置、联系人选择等场景。实现ListView的多选机制涉及到对Android的Adapter、CheckBox、OnItemClickListener以及选择状态的管理。...

    CheckBox多选功能的实现

    在Android开发中,CheckBox是用户界面(UI)中常见...这种技术在很多场景下都很实用,比如在设置界面让用户选择多项功能,或者在联系人列表中选择多个联系人。通过熟练掌握这种技巧,你可以提高Android应用的用户体验。

    Android自定义Dialog多选对话框(Dialog+Listview+CheckBox)

    在Android开发中,自定义Dialog...这种功能在很多场景下都很实用,比如选择联系人、文件或者其他任何需要用户进行多项选择的操作。开发者需要掌握Dialog的创建、ListView的使用以及适配器的编写,才能实现这样的功能。

    android listview,ExpandableListView实现单选,多选,全选,edittext实现批量输入

    它们经常被用于构建用户界面,比如联系人列表或者菜单项。在这个教程中,我们将深入探讨如何在这两种列表中实现单选、多选以及全选功能,并结合EditText实现批量输入。 1. **ListView实现单选**: 在ListView中...

    Android里ListView里面添加CheckBox实现多选

    这个功能在各种应用中非常常见,例如邮件客户端、联系人管理器等。本篇文章将深入探讨如何在ListView中实现CheckBox的多选功能,并使用ViewHolder和ConvertView优化性能。 1. **ListView与Adapter基础** - ...

    Android ListView快速筛选联系人

    本主题“Android ListView快速筛选联系人”主要涉及如何利用ListView实现高效的联系人筛选功能,同时结合搜索输入框进行实时匹配。下面将详细讲解这个过程。 首先,我们需要一个包含联系人信息的数据模型。通常,每...

    ListView实现单选多选

    这在很多应用场景中非常常见,例如在设置菜单、联系人选择等场景。 首先,我们需要理解ListView的基本工作原理。ListView通过Adapter将数据与视图进行绑定,Adapter负责将数据源转换为ListView可以显示的View。在这...

    Android-Android联系人选择器

    总结来说,实现"Android-Android联系人选择器"涉及到以下关键点: 1. 请求`READ_CONTACTS`权限 2. 使用ContentResolver和ContactsContract查询联系人 3. 创建ListView/RecyclerView展示联系人 4. 实现多选功能,维护...

    Recyclerview实现多选功能

    在Android开发中,RecyclerView是一种非常常用的控件,用于展示大量...这种功能在很多应用场景中都非常实用,比如文件管理、联系人选择等。在实际开发中,还可以根据需求进一步定制,例如增加搜索、排序等高级功能。

    选择多个手机联系人并发送短信

    总结起来,实现“选择多个手机联系人并发送短信”功能,主要涉及到Android的联系人API、ListView组件、多选机制的实现以及SmsManager的使用。这个过程需要结合UI设计、数据操作和系统服务调用来完成。

    安卓Android源码——多选库.zip

    这个库可能是为了帮助开发者实现用户界面中的复选框或者选择列表,使得用户可以方便地进行多项选择操作,常见于如设置菜单、联系人选择等场景。 【描述】描述简单明了,表明这是一个与Android相关的源码包,重点...

    Android高级应用源码-实现列表多选的DEMO.zip

    在Android开发中,实现列表多选是一个常见的需求,特别是在创建如设置界面、联系人选择或者文件管理器等应用时。这个"Android高级应用源码-实现列表多选的DEMO.zip"应该包含了一个完整的示例项目,展示了如何在...

    Android高级应用源码-实现列表多选的DEMO.rar

    在Android开发中,实现列表多选是一个常见的需求,特别是在创建具有交互性的应用时,比如设置界面、联系人选择等。这个"Android高级应用源码-实现列表多选的DEMO"是一个很好的学习资源,它展示了如何在Android环境中...

    GridView实现多选,全选,反选功能

    在很多场合,我们需要实现GridView的多选、全选和反选功能,这在构建诸如联系人选择、图片选择等场景时非常实用。本篇文章将详细讲解如何在Android中实现GridView的这些高级功能。 首先,我们需要理解GridView的...

    开源项目——MultiChoiceAdapter(可多选的适配器)

    2. **联系人应用**:在联系人应用中,用户可能需要选择多个联系人进行群发消息或添加到群组。 3. **购物车**:在电商应用中,用户可以选择多个商品加入购物车。 4. **消息管理**:如上述描述,邮件应用中多选邮件...

    Android 一个批量删除联系人的Demo-IT计算机-毕业设计.zip

    5. **多选机制**:为了实现批量删除,应用需要支持用户多选联系人。这可以通过实现CheckBox或者Switch控件实现,同时配合监听事件来追踪选中的联系人。 6. **确认对话框**:在执行删除操作之前,通常会弹出一个确认...

    EditText 输入仿发短信选择多个联系人,

    在Android开发中,创建一个类似于发送短信时选择多个联系人的功能是一项常见的需求。这个功能通常涉及到EditText组件的自定义和优化,使其能够显示并编辑多段文本,每段文本可以独立进行操作,如校验、删除或添加。...

Global site tag (gtag.js) - Google Analytics