2011-04-21 54 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));