2013-03-25 69 views
4

我想選擇從手機可用的接觸編程,我使用下面的代碼如何使用複選框

 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
     intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
     startActivityForResult(intent, 1); 

然而,問題是如何選擇在多個聯繫人中選擇從手機的多個聯繫人時間通過使用聯繫人頁面中的複選框?

+0

也許這會有所幫助:https://github.com/1gravity/Android-ContactPicker(我是作者) – 2016-05-28 06:44:42

回答

10

您將不得不以編程方式閱讀聯繫人,並在您的Activity中的ListView中顯示它們。在ListView項目中使用CheckBox s,並允許選擇多個項目。找到一個簡單的示例/教程ListView並從那裏開始。

有幾個原因,最好是創建一個自定義ListView而不是使用Intent(Intent.ACTION_GET_CONTENT);

  1. 可能沒有辦法選擇你要求的倍數。
  2. 即使您找到了選擇倍數的方法,它在每個操作系統版本和設備上的 上也會不同,並且可能不適用於所有倍數。
  3. 如果沒有安裝,可以 處理ACTION_GET_CONTENT任何設備上的多個應用程序,那麼選擇器將呈現給用戶 ,他將不得不選擇其中的一個。用戶的選擇 可能不支持選擇多個聯繫人。

這裏是讀取系統通訊錄的例子:

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
while (cursor.moveToNext()) { 
    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
    String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
    if("1".equals(hasPhone) || Boolean.parseBoolean(hasPhone)) { 
     // You know it has a number so now query it like this 
     Cursor phones = myActivity.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
     while (phones.moveToNext()) { 
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      int itype = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 

      final boolean isMobile = 
       itype == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE || 
       itype == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE; 

      // Do something here with 'phoneNumber' such as saving into 
      // the List or Array that will be used in your 'ListView'. 

     } 
     phones.close(); 
    } 
} 
+0

下面的答案是偉大的沒有自定義listview :)看到@Gdroid – EranLevi 2016-01-23 09:19:51

3
public static final int REQUEST_CODE_PICK_CONTACT = 1; 
    public static final int MAX_PICK_CONTACT= 10; 

    private void launchMultiplePhonePicker() { 

     Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU"); 
     phonebookIntent.putExtra("additional", "phone-multi"); 
     phonebookIntent.putExtra("maxRecipientCount", MAX_PICK_CONTACT); 
     phonebookIntent.putExtra("FromMMS", true); 
     startActivityForResult(phonebookIntent, REQUEST_CODE_PICK_CONTACT); 

    } 



@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if(resultCode==RESULT_OK) 
    { 

    if(requestCode == REQUEST_CODE_PICK_CONTACT ) 
    { 

     Bundle bundle = data.getExtras(); 

     String result= bundle.getString("result"); 
     ArrayList<String> contacts = bundle.getStringArrayList("result"); 


     Log.i(TAG, "launchMultiplePhonePicker bundle.toString()= " + contactsPick.toString()); 

     } 
    } 

    super.onActivityResult(requestCode, resultCode, data); 
} 
+0

終於...你讓我的一天:),謝謝你! – EranLevi 2016-01-23 09:17:35