2011-04-21 52 views
0

我能夠成功啓動聯繫人選取器,然後將我的活動中的editText設置爲所選聯繫人的電話號碼。我現在想要做的是將另外兩個editText分別設置爲名字和姓氏。我添加了其他editTexts並試圖檢索最後一個名字。使用聯繫人選取器獲取多個數據

這是我有:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     switch (requestCode) { 
     case CONTACT_PICKER_RESULT: 
      Cursor cursor = null; 
      String number = ""; 
      String lastName = ""; 
      String firstName = ""; 
      try { 
       Uri result = data.getData(); 
       Log.v(DEBUG_TAG, "Got a contact result: " 
         + result.toString()); 

       // get the contact id from the Uri 
       String id = result.getLastPathSegment(); 

       // query for phone number 
       cursor = getContentResolver().query(Phone.CONTENT_URI, 
         null, Phone.CONTACT_ID + "=?", new String[] { id }, 
         null); 

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

       // get the phone number 
       if (cursor.moveToFirst()) { 
        number = cursor.getString(phoneIdx); 
        lastName = cursor.getString(lastNameIdx); 
        firstName = cursor.getString(firstNameIdx); 


        Log.v(DEBUG_TAG, "Got number " + number); 
       } else { 
        Log.w(DEBUG_TAG, "No results"); 
       } 
      } catch (Exception e) { 
       Log.e(DEBUG_TAG, "Failed to get phone number data", e); 
      } finally { 
       if (cursor != null) { 
        cursor.close(); 
       } 
       EditText phoneNumberEditText = (EditText) findViewById(R.id.number); 
       phoneNumberEditText.setText(number); 
       EditText lastNameEditText = (EditText)findViewById(R.id.last_name); 
       lastNameEditText.setText(lastName); 
       EditText firstNameEditText = (EditText)findViewById(R.id.first_name); 
       firstNameEditText.setText(firstName); 
       if (number.length() == 0) { 
        Toast.makeText(this, "No phone number found for this contact.", 
          Toast.LENGTH_LONG).show(); 
       } 
       if(lastName.length()==0) { 
        Toast.makeText(this, "No last name found for this contact.", 
          Toast.LENGTH_LONG).show(); 
       } 
       if(firstName.length()==0) { 
        Toast.makeText(this, "No first name found for this contact.", 
          Toast.LENGTH_LONG).show(); 
       } 

      } 

      break; 
     } 

    } else { 
     Log.w(DEBUG_TAG, "Warning: activity result not ok"); 
    } 
} 

我必須缺少基本的東西。有什麼想法嗎?

回答

0

試試這個:

String lastName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)) 
+0

不起作用。該應用程序不會崩潰,但我得到的是電話號碼,而不是名稱... – LuxuryMode 2011-04-22 04:03:49

+0

放入一對Log.d()語句並觀察logCat或在調試模式下逐步找出正在分配的內容到那些變量。敬酒也工作。 – 2011-04-22 11:19:51

0

如果你調用一個managedQuery它會返回聯繫人的全名和ID,但沒有電話號碼。

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