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

[html]   view plain copy print ?
  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>  

猜你喜欢

转载自technicalsearch.iteye.com/blog/2033759