天天看點

android多選聯系人實作

有很多網友問多選聯系人實作方式,這裡參考了apidemos的例子做了簡單實作。

<a href="http://blog.51cto.com/attachment/201203/224551404.gif" target="_blank"></a>

整體思路是使用使用一個ArrayList存放選中的聯系人資訊,細節就不說了,貼一下代碼

public class CopyContactsListMultiple extends ListActivity implements OnClickListener{  

    private final int UPDATE_LIST=1;  

    ArrayList&lt;String&gt; contactsList; //得到的所有聯系人  

    ArrayList&lt;String&gt; 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&lt;String&gt;();  

        getcontactsList=new ArrayList&lt;String&gt;();  

        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&lt;String&gt;(this,  

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

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

        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("[")&gt;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() {  

           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()&gt;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();  

}  

protected void onDestroy() {  

    contactsList.clear();  

    getcontactsList.clear();  

    super.onDestroy();  

public void onClick(View v) {  

    switch (v.getId()) {  

    case R.id.contacts_done_button:  

           Intent i = new Intent();   

           if(getcontactsList!=null&amp;&amp;getcontactsList.size()&gt;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();  

    default:  

public boolean onKeyDown(int keyCode, KeyEvent event) {  

    if(keyCode==KeyEvent.KEYCODE_BACK){  

            Bundle b = new Bundle();    

            b.putStringArrayList("GET_CONTACT", getcontactsList);  

            i.putExtras(b); // }  

    return super.onKeyDown(keyCode, event);  

&lt;?xml version="1.0" encoding="utf-8"?&gt; 

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

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

    android:layout_height="fill_parent"&gt; 

    &lt;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"&gt; 

    &lt;/ListView&gt; 

    &lt;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"&gt; 

        &lt;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" /&gt; 

        &lt;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" /&gt; 

    &lt;/LinearLayout&gt; 

&lt;/LinearLayout&gt; 

     本文轉自xyz_lmn51CTO部落格,原文連結:http://blog.51cto.com/xyzlmn/816841,如需轉載請自行聯系原作者

繼續閱讀