2010-09-10 69 views
3

我試圖創建一個「select-multiple」聯繫人列表,我可以讓用戶檢查多個聯繫人。我正在尋找的東西與向多個聯繫人撰寫郵件時出現的本地活動實際上是一樣的。謝謝!Android Contact Picker Checklist

+0

試圖與ContactsContract的東西,但1.6 compat的將是很好。我的問題是與複選框部分。在消息傳遞中,Android是否具有某種帶有複選框的本地聯繫人選擇器? – Will 2010-09-11 21:01:04

回答

1

首先,您需要獲取聯繫人列表,然後將其顯示在ListView元素上。比如我用下面的代碼來顯示所有用戶在一個ListView聯繫人:

    // Run query on all contacts id 
       Uri uri = ContactsContract.Contacts.CONTENT_URI; 
       String[] projection = new String[] { ContactsContract.Contacts._ID, 
               ContactsContract.Contacts.DISPLAY_NAME}; 
       String selection = null;//ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '" + ("1") + "'"; 
       String[] selectionArgs = null; 
       String sortOrder = ContactsContract.Contacts.DISPLAY_NAME 
         + " COLLATE LOCALIZED ASC"; 

       ContentResolver contectResolver = getContentResolver(); 

       Cursor cursor = contectResolver.query(uri, projection, selection, selectionArgs, 
         sortOrder); 

       //Create buffer 
       final ArrayList<ContactData> bufferContacts = new ArrayList<ContactData>(); 

       //Load contacts one by one 
       if(cursor.moveToFirst()) { 
        while(!cursor.isAfterLast()) { 



         String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 


         String[] emailProj = new String[]{Email.DATA}; 
         Cursor cursorEmail = contectResolver.query(Email.CONTENT_URI, emailProj,Email.CONTACT_ID + " = ? ", new String[] { id }, null); 

         String[] phoneProj = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}; 
         Cursor cursorPhone = contectResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, phoneProj, 
           ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); 

         String firstName = ""; 
         String lastName = ""; 
         String email = ""; 
         String displayname = ""; 
         String phoneNmb = ""; 

         if(cursorPhone.moveToFirst()) { 
          ///displayname = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
          phoneNmb = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         } 
         cursorPhone.close(); 

         if(cursorEmail.moveToFirst()) { 
          email = cursorEmail.getString(cursorEmail.getColumnIndex(Email.DATA)); 
         } 
         cursorEmail.close(); 

         displayname = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

         //Divide display name to first and last 
         String[] names = displayname.split("\\s+"); 

         firstName = displayname; 

         if(names.length >= 1) { 
          firstName = names[0]; 
         } 

         if(names.length >= 2) { 
          firstName = names[1]; 
         } 

         final ContactData contactData = new ContactData(id, firstName, lastName, phoneNmb, email, allChecked); 

         bufferContacts.add(contactData); 

         //Set list view initialy 
         runOnUiThread(new Runnable() { 
          public void run() { 

           if(contactsAdapter == null) { 
            ArrayList<ContactData> contacts = new ArrayList<ContactData>(); 
            contactsAdapter = new ContactAdapter(ContactPickerActivity.this, contacts); 

            lvContacts.setAdapter(contactsAdapter); 
           } 

           if(bufferContacts.size() >= BUFFER_INTERVAL) { 
            addBuffer(bufferContacts); 
           } 
          } 

         }); 

         cursor.moveToNext(); 
        } 
       } 

       cursor.close(); 

       runOnUiThread(new Runnable() { 
        public void run() { 

         addBuffer(bufferContacts); 

        } 

       }); 

如果你不想打擾一下讓一切從頭開始出來的那麼隨意使用我準備庫選擇多聯繫人:

https://github.com/kgadzinowski/Android-Multiple-Contacts-Picker-Library

+0

你還沒有提到如何使用你的庫和支持gradle,它會很容易使用和幫助。 – 2015-09-26 13:21:49