您的位置:首页 > 移动开发 > Android开发

android多选联系人实现

2013-11-11 00:54 423 查看
     有很多网友问多选联系人实现方式,这里参考了apidemos的例子做了简单实现。



 

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

 

 

[java] view
plaincopyprint?

public class CopyContactsListMultiple extends ListActivity implements OnClickListener{  

      

    private final int UPDATE_LIST=1;  

    ArrayList<String> contactsList; //得到的所有联系人  

    ArrayList<String> getcontactsList; //选择得到联系人  

    private Button okbtn;  

    private Button cancelbtn;  

    private ProgressDialog proDialog;  

  

    Thread getcontacts;  

    Handler updateListHandler = new Handler() {  

            public void handleMessage(Message msg) {  

                 switch (msg.what) {  

                   

                 case UPDATE_LIST:  

                     if (proDialog != null) {  

                            proDialog.dismiss();  

                        }  

                     updateList();   

                 }  

            }  

     };  

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.contactslist);  

        contactsList=new ArrayList<String>();  

        getcontactsList=new ArrayList<String>();  

  

        final ListView listView = getListView();  

        listView.setItemsCanFocus(false);  

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);  

        okbtn=(Button)findViewById(R.id.contacts_done_button);  

        cancelbtn=(Button)findViewById(R.id.contact_back_button);  

        okbtn.setOnClickListener(this);  

        cancelbtn.setOnClickListener(this);  

          

        getcontacts=new Thread(new GetContacts());  

        getcontacts.start();  

        proDialog = ProgressDialog.show(CopyContactsListMultiple.this, "loading","loading", true, true);  

  

    }  

      

    @Override  

    protected void onResume() {  

        // TODO Auto-generated method stub  

        super.onResume();  

              

    }  

  

  

    void updateList(){  

        if(contactsList!=null)  

        setListAdapter(new ArrayAdapter<String>(this,  

               android.R.layout.simple_list_item_multiple_choice, contactsList));  

          

    }  

  

      

    @Override  

    protected void onListItemClick(ListView l, View v, int position, long id) {  

        // TODO Auto-generated method stub  

        if(!((CheckedTextView)v).isChecked()){  

              

            CharSequence num=((CheckedTextView)v).getText();  

            getcontactsList.add(num.toString());  

        }  

            if(((CheckedTextView)v).isChecked()){     

                CharSequence num=((CheckedTextView)v).getText();  

                if((num.toString()).indexOf("[")>0){  

                    String phoneNum=num.toString().substring(0, (num.toString()).indexOf("\n"));  

                    getcontactsList.remove(phoneNum);  

                    Log.d("remove_num", ""+phoneNum);  

                    }else{  

                         getcontactsList.remove(num.toString());  

                        Log.d("remove_num", ""+num.toString());  

                    }  

        }  

        super.onListItemClick(l, v, position, id);    

    }  

   class GetContacts implements Runnable{  

       @Override  

    public void run() {  

        // TODO Auto-generated method stub  

           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 cursor=managedQuery(uri, projection, selection, selectionArgs, sortOrder);  

            Cursor phonecur = null;  

  

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

                phonecur = managedQuery(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, android.provider.ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "  + contactId, null, null);     

                // 取得电话号码(可能存在多个号码)    

                while (phonecur.moveToNext()){    

                    String strPhoneNumber = phonecur.getString(phonecur.getColumnIndex(android.provider.ContactsContract.CommonDataKinds.Phone.NUMBER));    

                    if(strPhoneNumber.length()>4)  

                        contactsList.add("18610011001"+"\n测试");  

                    //contactsList.add(strPhoneNumber+"\n"+name+"");  

              

                }      

            }    

            if(phonecur!=null)  

             phonecur.close();  

            cursor.close();   

  

            Message msg1=new Message();  

            msg1.what=UPDATE_LIST;  

            updateListHandler.sendMessage(msg1);  

    }   

   }  

@Override  

protected void onPause() {  

    // TODO Auto-generated method stub  

    super.onPause();  

      

}  

  

@Override  

protected void onDestroy() {  

    contactsList.clear();  

    getcontactsList.clear();  

    super.onDestroy();  

}  

  

  

@Override  

public void onClick(View v) {  

    // TODO Auto-generated method stub  

    switch (v.getId()) {  

    case R.id.contacts_done_button:  

           Intent i = new Intent();   

           if(getcontactsList!=null&&getcontactsList.size()>0){  

               Bundle b = new Bundle();    

               b.putStringArrayList("GET_CONTACT", getcontactsList);  

               i.putExtras(b);    

            }  

            setResult(RESULT_OK, i);    

            CopyContactsListMultiple.this.finish();  

        break;  

    case R.id.contact_back_button:  

        CopyContactsListMultiple.this.finish();  

        break;  

    default:  

        break;  

    }  

}  

@Override  

public boolean onKeyDown(int keyCode, KeyEvent event) {  

    // TODO Auto-generated method stub  

    if(keyCode==KeyEvent.KEYCODE_BACK){  

           Intent i = new Intent();   

            Bundle b = new Bundle();    

            b.putStringArrayList("GET_CONTACT", getcontactsList);  

            i.putExtras(b); // }  

            setResult(RESULT_OK, i);    

    }  

    return super.onKeyDown(keyCode, event);  

}  

}  

[html] view
plaincopyprint?

<?xml version="1.0" encoding="utf-8"?>  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:orientation="vertical" android:layout_width="fill_parent"  

    android:layout_height="fill_parent">  

  

  

    <ListView android:id="@+id/android:list"   

        android:layout_height="fill_parent"   

        android:layout_width="fill_parent"  

         android:layout_marginLeft="10dip"  

        android:layout_marginRight="10dip"   

        android:layout_marginTop="10dip"  

        android:layout_weight="1.0">  

    </ListView>  

  

    <LinearLayout android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:layout_weight="0" android:orientation="horizontal"  

        android:gravity="center" android:layout_marginLeft="10dip"  

        android:layout_marginRight="10dip" android:layout_marginBottom="10dip"  

        android:weightSum="1">  

  

        <Button android:id="@+id/contacts_done_button"  

            android:textSize="17dip"  

            android:layout_marginRight="10dip" android:layout_width="0dip"  

            android:layout_height="wrap_content" android:layout_weight="0.35"  

            android:text="sure" />  

  

        <Button android:id="@+id/contact_back_button"  

            android:layout_marginLeft="10dip" android:textSize="17dip"  

            android:layout_width="0dip" android:layout_height="wrap_content"  

            android:layout_weight="0.35" android:text="back" />  

    </LinearLayout>  

   

</LinearLayout>  
原作者:http://blog.csdn.net/xyz_lmn/article/details/7235913
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: