2011-04-04 105 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

相關問題