2016-01-21 66 views
0

我想获得特定名称,姓氏等在android上的特定联系人,但似乎无法做到。几个小时以来,我一直在抨击我的头,基本上if (nameCur.moveNext())总是错误的!此代码最初由@perborin(How to get the first name and last name from Android contacts?)提供。请帮忙!获取具体的联系信息

P.S.我在AndroidManifest中添加了<uses-permission android:name="android.permission.READ_CONTACTS"/>,所以这不是问题。

// A contact ID is fetched from ContactList 
Uri resultUri = data.getData(); 
Cursor cont = getContentResolver().query(resultUri, null, null, null, null); 
if (!cont.moveToNext()) { 
    Toast.makeText(this, "Cursor contains no data", Toast.LENGTH_LONG).show(); 
       return; 
} 
int columnIndexForId = cont.getColumnIndex(ContactsContract.Contacts._ID); 
String contactId = cont.getString(columnIndexForId); 

// Fetch contact name with a specific ID 
String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = " + contactId; 
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE }; 
Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 
while (nameCur.moveToNext()) { 
    String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)); 
    String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)); 
    String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME)); 
    Toast.makeText(this, "Name: " + given + " Family: " + family + " Displayname: " + display, Toast.LENGTH_LONG).show(); 
} 
nameCur.close(); 
cont.close(); 

回答

0

通过参考用户3717188解决的答案在Get first and last name of a contact rather than single display name?

Cursor phone_cursor = cr.query(ContactsContract.CommonDataKinds. 
       Phone.CONTENT_URI, null, null, null, null); 
     while (phone_cursor.moveToNext()) { 
      try { 
      int id = Integer.parseInt(phone_cursor.getString(phone_cursor.getColumnIndex 
        (ContactsContract.CommonDataKinds.Phone.CONTACT_ID))); 
      Cursor name_cursor = cr.query(ContactsContract.Data.CONTENT_URI,null, 
        ContactsContract.Data.CONTACT_ID + " = " + id, null, null); 

       String name = phone_cursor.getString(phone_cursor.getColumnIndex 
         (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
       String first_name =""; 
       String last_name = ""; 
       while (name_cursor.moveToNext()) { 
        if(name_cursor.getString(name_cursor.getColumnIndex 
          (ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))!=null){ 
        first_name = name_cursor.getString(name_cursor.getColumnIndex 
          (ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)); 
        last_name = name_cursor.getString(name_cursor.getColumnIndex 
          (ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)); 
       }} 
       name_cursor.close(); 
       String phoneNumber = phone_cursor.getString(phone_cursor.getColumnIndex 
         (ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      } catch (Exception e) { 
      } 
     } 
     phone_cursor.close();