有很多网友问多选联系人实现方式,这里参考了apidemos的例子做了简单实现。
整体思路是使用使用一个ArrayList存放选中的联系人信息,细节就不说了,贴一下代码
  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.     Thread getcontacts;
  10.         Handler updateListHandler = new Handler() {
  11.                         public void handleMessage(Message msg) {
  12.                                  switch (msg.what) {
  13.                                  
  14.                                  case UPDATE_LIST:
  15.                                          if (proDialog != null) {
  16.                                                         proDialog.dismiss();
  17.                                                 }
  18.                                          updateList();
  19.                                  }
  20.                         }
  21.          };
  22.         public void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.contactslist);
  25.         contactsList=new ArrayList<String>();
  26.         getcontactsList=new ArrayList<String>();

  27.         final ListView listView = getListView();
  28.         listView.setItemsCanFocus(false);
  29.         listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  30.         okbtn=(Button)findViewById(R.id.contacts_done_button);
  31.         cancelbtn=(Button)findViewById(R.id.contact_back_button);
  32.         okbtn.setOnClickListener(this);
  33.         cancelbtn.setOnClickListener(this);
  34.         
  35.                 getcontacts=new Thread(new GetContacts());
  36.             getcontacts.start();
  37.         proDialog = ProgressDialog.show(CopyContactsListMultiple.this, "loading","loading", true, true);

  38.         }
  39.        
  40.     @Override
  41.         protected void onResume() {
  42.                 // TODO Auto-generated method stub
  43.                 super.onResume();
  44.                     
  45.         }


  46.         void updateList(){
  47.                 if(contactsList!=null)
  48.             setListAdapter(new ArrayAdapter<String>(this,
  49.                android.R.layout.simple_list_item_multiple_choice, contactsList));
  50.            
  51.     }

  52.    
  53.     @Override
  54.         protected void onListItemClick(ListView l, View v, int position, long id) {
  55.                 // TODO Auto-generated method stub
  56.             if(!((CheckedTextView)v).isChecked()){
  57.                    
  58.                 CharSequence num=((CheckedTextView)v).getText();
  59.                 getcontactsList.add(num.toString());
  60.             }
  61.                 if(((CheckedTextView)v).isChecked()){       
  62.                         CharSequence num=((CheckedTextView)v).getText();
  63.                     if((num.toString()).indexOf("[")>0){
  64.                             String phoneNum=num.toString().substring(0, (num.toString()).indexOf("\n"));
  65.                         getcontactsList.remove(phoneNum);
  66.                         Log.d("remove_num", ""+phoneNum);
  67.                         }else{
  68.                                  getcontactsList.remove(num.toString());
  69.                                 Log.d("remove_num", ""+num.toString());
  70.                         }
  71.             }
  72.                 super.onListItemClick(l, v, position, id);       
  73.         }
  74.    class GetContacts implements Runnable{
  75.            @Override
  76.         public void run() {
  77.                 // TODO Auto-generated method stub
  78.                Uri uri = ContactsContract.Contacts.CONTENT_URI;
  79.                    String[] projection = new String[] {
  80.                         ContactsContract.Contacts._ID,
  81.                         ContactsContract.Contacts.DISPLAY_NAME,
  82.                         ContactsContract.Contacts.PHOTO_ID
  83.                 };
  84.                 String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
  85.                 String[] selectionArgs = null;
  86.                 String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
  87.                 Cursor cursor=managedQuery(uri, projection, selection, selectionArgs, sortOrder);
  88.                 Cursor phonecur = null;

  89.                         while (cursor.moveToNext()){  
  90.                                
  91.                             // 取得联系人名字  
  92.                             int nameFieldColumnIndex = cursor.getColumnIndex(android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME);  
  93.                             String name = cursor.getString(nameFieldColumnIndex);  
  94.                             // 取得联系人ID  
  95.                             String contactId = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID));  
  96.                             phonecur = managedQuery(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "  + contactId, null, null);   
  97.                             // 取得电话号码(可能存在多个号码)  
  98.                             while (phonecur.moveToNext()){  
  99.                                 String strPhoneNumber = phonecur.getString(phonecur.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));  
  100.                                 if(strPhoneNumber.length()>4)
  101.                                         contactsList.add("18610011001"+"\n测试");
  102.                                 //contactsList.add(strPhoneNumber+"\n"+name+"");
  103.                 
  104.                             }   
  105.                         }  
  106.                         if(phonecur!=null)
  107.                          phonecur.close();
  108.                         cursor.close();

  109.                         Message msg1=new Message();
  110.                         msg1.what=UPDATE_LIST;
  111.                         updateListHandler.sendMessage(msg1);
  112.         }
  113.    }
  114. @Override
  115. protected void onPause() {
  116.         // TODO Auto-generated method stub
  117.         super.onPause();
  118.        
  119. }

  120. @Override
  121. protected void onDestroy() {
  122.         contactsList.clear();
  123.         getcontactsList.clear();
  124.     super.onDestroy();
  125. }


  126. @Override
  127. public void onClick(View v) {
  128.         // TODO Auto-generated method stub
  129.         switch (v.getId()) {
  130.         case R.id.contacts_done_button:
  131.                    Intent i = new Intent();
  132.                 if(getcontactsList!=null&&getcontactsList.size()>0){
  133.                         Bundle b = new Bundle();  
  134.                b.putStringArrayList("GET_CONTACT", getcontactsList);
  135.                i.putExtras(b);  
  136.             }
  137.             setResult(RESULT_OK, i);  
  138.                         CopyContactsListMultiple.this.finish();
  139.                 break;
  140.         case R.id.contact_back_button:
  141.                 CopyContactsListMultiple.this.finish();
  142.                 break;
  143.         default:
  144.                 break;
  145.         }
  146. }
  147. @Override
  148. public boolean onKeyDown(int keyCode, KeyEvent event) {
  149.         // TODO Auto-generated method stub
  150.         if(keyCode==KeyEvent.KEYCODE_BACK){
  151.                 Intent i = new Intent();
  152.             Bundle b = new Bundle();  
  153.             b.putStringArrayList("GET_CONTACT", getcontactsList);
  154.             i.putExtras(b); // }
  155.             setResult(RESULT_OK, i);  
  156.         }
  157.         return super.onKeyDown(keyCode, event);
  158. }
  159. }
复制代码
  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>
复制代码
不要消极的等,一切都要主动争取,别人不会为你上心,除非你一直提醒他,并明确表示意愿。
实习工程师 2# qiangqiangci 发表于 2012-2-6 20:26:08
没图没真相

点评

详情 回复 xyz_lmn  发表于 2012-2-6 20:26:48

唉,刚在上传图呢,你也太速度了。
版主 3# xyz_lmn 发表于 2012-2-6 20:26:15
device-2012-02-06-091606.png
不要消极的等,一切都要主动争取,别人不会为你上心,除非你一直提醒他,并明确表示意愿。
版主 4# xyz_lmn 发表于 2012-2-6 20:26:48
qiangqiangci 发表于 2012-2-6 20:26
没图没真相

唉,刚在上传图呢,你也太速度了。

点评

详情 回复 David_Zhang  发表于 2012-2-6 22:08:41

他是求知若渴,理解万岁~~ 给力啊~~
不要消极的等,一切都要主动争取,别人不会为你上心,除非你一直提醒他,并明确表示意愿。
版主 5# David_Zhang 发表于 2012-2-6 22:08:41
xyz_lmn 发表于 2012-2-6 20:26
唉,刚在上传图呢,你也太速度了。

他是求知若渴,理解万岁~~
给力啊~~
~~阿凡达这部电影告诉我们,谁胯下的鸟大听谁的~~
城管队长 6# Sodino 发表于 2012-2-6 23:27:45
http://www.sodino.com/AndroidNotepad/AndroidNotepad.html
见习构架师 7# lujunfeng 发表于 2012-2-7 09:00:31
学习
高级工程师 8# gaokai 发表于 2012-2-7 09:22:17
牛人啊
版主 9# xyz_lmn 发表于 2012-2-7 11:05:29
gaokai 发表于 2012-2-7 09:22
牛人啊

:lol
不要消极的等,一切都要主动争取,别人不会为你上心,除非你一直提醒他,并明确表示意愿。
初级构架师 10# xyuan52021 发表于 2012-2-18 18:07:10
确实够强大 学习一下
高级工程师 11# samcheng 发表于 2012-3-8 15:06:05
Thank you very much
实习工程师 12# CDFQ 发表于 2012-3-8 23:34:35
值得學習 多選擇的方法 來更新好用的功能
实习工程师 13# gfansenhua 发表于 2012-3-21 21:51:46
看起来还是不错的!!

点评

详情 回复 xyz_lmn  发表于 2012-3-22 08:33:22

恩,加上排序会更好。
版主 14# xyz_lmn 发表于 2012-3-22 08:33:22
gfansenhua 发表于 2012-3-21 21:51
看起来还是不错的!!

恩,加上排序会更好。
不要消极的等,一切都要主动争取,别人不会为你上心,除非你一直提醒他,并明确表示意愿。
初级工程师 15# devlee 发表于 2012-4-17 12:15:48
感谢分享!!!!
研究员 16# malinadevdiv 发表于 2012-5-8 10:53:00
年底时间过的好慢有木有~~~~
您需要登录后才可以回帖 登录 | 注册

关于我们|手机版|Archiver|DEVDIV.COM ( 京ICP备07040843号 )  

GMT+8, 2012-5-19 17:18

Powered by DEVDIV.COM!

© 2010-2012 DEVDIV.COM Coummunity.

回顶部