【问题标题】:Getting specific contact information获取具体联系信息
【发布时间】:2016-01-21 17:39:26
【问题描述】:

我正在尝试在 android 上获取特定联系人的名字、姓氏等,但似乎无法获得。几个小时以来我一直在努力解决这个问题,基本上if (nameCur.moveNext()) 总是错误的!此代码最初由 @perborin (How to get the first name and last name from Android contacts?) 提供。请帮忙!

附:我在 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();

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    通过参考Get first and last name of a contact rather than single display name?的user3717188答案解决。

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2012-02-21
      相关资源
      最近更新 更多