2011-11-11 73 views
-1

我已經開始接觸拾取活動我如何獲得名字和姓從聯繫人選擇器

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
        startActivityForResult(intent, enumRequestCode.PICK_CONTACT.ordinal()); 

然後在onActivityResult我有這樣的代碼來獲取電話號碼(而DISPLAY_NAME,但說不好,不夠...我需要名字和姓氏能夠支持不同國家的投入):

// String firstName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)); 
// String surName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)); 

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

     if(enumRequestCode.PICK_CONTACT.ordinal() == requestCode){ 
       if (resultCode == Activity.RESULT_OK) { 
        Cursor cursor = managedQuery(data.getData(), null, null, null, null);  
        while (cursor.moveToNext()) 
        {   
         String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
         String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

         String phoneNumber = ""; 
         int hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 


         String firstName = ""; 
         String surName = ""; 


          if (hasPhone >= 1){ 
           Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
           phones.moveToNext(); 
           phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 



    phones.close(); 
         } 

我已經試過

但這並沒有奏效。

我已經看到了這一個,但它並沒有真正幫助我很多: How to get the first name and last name from Android contacts?

任何人都可以幫助我嗎?

在此先感謝!

回答

0
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);  
     while (cursor.moveToNext()) {   
      String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
      name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

      String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

      if (hasPhone.equalsIgnoreCase("1")) 
       hasPhone = "true"; 
      else 
       hasPhone = "false" ; 

      if (Boolean.parseBoolean(hasPhone)) { 
       Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
       if(phones.getCount() > 1) { 
        ArrayList<String> p = new ArrayList<String>(); 

        while (phones.moveToNext()) { 
         p.add(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 
        } 
        final CharSequence[] items = p.toArray(new CharSequence[p.size()]); 
        AlertDialog.Builder builder = new AlertDialog.Builder(newContact.this); 
        builder.setItems(items, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int item) { 
          phoneNumber = (String) items[item]; 
          showContentSettings(); 
         } 
        }); 
        AlertDialog alert = builder.create(); 
        alert.show(); 
       }else { 
        phones.moveToFirst(); 
        phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        showContentSettings(); 
       } 
       phones.close(); 
      }else { 
       showDialog(DIALOG_PHONE_NOT_FOUND); 
      } 
     } 
     cursor.close(); 
     Log.d(TAG,"name: " + name + "\nphoneNumber: " + phoneNumber); 
+0

呃,我已經得到了電話號碼,但是我需要名字和姓氏。它看起來不像你提供了什麼? – Ikky

+0

解析電子名片數據 vcard = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.CONTENT_VCARD_TYPE)); http://ru.wikipedia.org/wiki/VCard – alezhka

0
cursor = getContentResolver().query(Data.CONTENT_URI, 
        new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL}, 
        Data.CONTACT_ID + "=?" + " AND " 
          + Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'", 
        new String[] {String.valueOf(id)}, null); 

     int lastNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME); 
     int firstNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 

這是我如何做它的名字和姓氏...確實其他形式的名稱與MIMETYPE = StructuredName.CONTENT_ITEM_TYPE

相關問題