2011-04-04 107 views
2

联系人可能有许多电话号码(手机,家庭,..)。我想让用户选择一个特定联系人的电话号码。startActivityForResult的一个联系人的所有电话号码

有了这个片段,我得到了每个联系人的所有电话号码列表。

Intent intent = new Intent(Intent.ACTION_PICK, 
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI); 
startActivityForResult(intent, PHONE_NUMBER_PICKED); 

如何只列出一个联系人的电话号码?

编辑:我知道如何获取联系人的所有电话号码,这不是重点。我可以把所有的电话号码放在列表视图中,让用户选择一个。但是这个功能存在(上面提到),我只是不想要所有的号码,但只有一个联系人的电话号码。

+0

请具体在您的问题。在你的“编辑”中,你是自相矛盾的。你能告诉我什么是“一个联系人的所有电话号码”和“一个联系人的电话号码”的差异btw? – mudit 2011-04-05 08:50:12

+0

对不起,如果不明确。两者对我来说都是一样的。我想过滤由ContactsContract.CommonDataKinds.Phone.CONTENT_URI给出的列表,以便显示的列表仅包含附加到给定联系人的电话号码。 – 2011-04-05 09:36:32

回答

3

,如果你想获得所有相关的联系人,则电话号码:

1)使用此意图打开通讯录应用程序:

Intent intent = new Intent(Intent.ACTION_PICK); 
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE); 
    startActivityForResult(intent, PICK_CONTACT); 

2)onActivityResult使用下面的代码:

if (requestCode == PICK_CONTACT) { 
      if (resultCode == Activity.RESULT_OK) { 
       if (data != null) { 
        Uri contactData = data.getData(); 

        try { 

         String id = contactData.getLastPathSegment(); 
         Cursor phoneCur = getContentResolver() 
           .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
             null, 
             ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
               + " = ?", new String[] { id }, 
             null); 

         final ArrayList<String> phonesList = new ArrayList<String>(); 
         while (phoneCur.moveToNext()) { 
          // This would allow you get several phone addresses 
          // if the phone addresses were stored in an array 
          String phone = phoneCur 
            .getString(phoneCur 
              .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)); 
          phonesList.add(phone); 
         } 
         phoneCur.close(); 

         if (phonesList.size() == 0) { 
          Helper.showToast(
            this, 
            getString(R.string.error_no_phone_no_in_contact), 
            Toast.LENGTH_LONG); 
         } else if (phonesList.size() == 1) { 
          editText.setText(phonesList.get(0)); 
         } else { 

          final String[] phonesArr = new String[phonesList 
            .size()]; 
          for (int i = 0; i < phonesList.size(); i++) { 
           phonesArr[i] = phonesList.get(i); 
          } 

          AlertDialog.Builder dialog = new AlertDialog.Builder(
            SendSMS.this); 
          dialog.setTitle(R.string.choose_phone); 
          ((Builder) dialog).setItems(phonesArr, 
            new DialogInterface.OnClickListener() { 
             public void onClick(
               DialogInterface dialog, 
               int which) { 
              String selectedEmail = phonesArr[which]; 
              editText.setText(selectedEmail); 
             } 
            }).create(); 
          dialog.show(); 
         } 
        } catch (Exception e) { 
         Log.e("FILES", "Failed to get phone data", e); 
        } 
       } 
      } 
     } 

这将在名为editText的编辑文本中设置选定的电话号码。您可以根据需要更改此设置。

+0

我喜欢在结果中使用超过1的警告对话框:-) – 2013-04-30 05:31:30

+0

我修改了代码以获取名称和联系人图片,但它不起作用。任何方式来帮助我?还有什么方法可以修改以显示它旁边的号码类型? – Si8 2013-10-10 03:17:57

+0

你介意看看这个吗? http://stackoverflow.com/questions/19286637/phone-numbers-along-with-name – Si8 2013-10-10 03:38:04

相关问题