4

我正在构建一个需要从 Android 联系人中选择联系人的应用程序。我这样做了,并在我的应用程序中使用以下代码来检索姓名和姓氏表格选择的联系人(在 中onActivityResult)。

Cursor namesc = getContentResolver().query( ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.CONTACT_ID + " = " + id,null, null);
if (namesc.moveToFirst()) {
String name = namesc.getString(namesc.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String family = namesc.getString(namesc
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
String middle = namesc.getString(namesc
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));

它在 HTC 手机和其他一些手机中运行良好,但在许多 SE Xperia 型号和一些三星设备中,它不能给我姓名和家庭并返回 null。有谁能够帮我?

4

3 回答 3

0

这是我使用的代码:

        String structuredNameWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
        String[] structuredNameWhereParams = new String[]{id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE}; 
        cursor = resolver.query(ContactsContract.Data.CONTENT_URI, null, structuredNameWhere, structuredNameWhereParams, null); 
        if (cursor.moveToFirst()) 
        { 
            String StructuredNameId = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName._ID));
            String prefix = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
            String givenName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
            String middleName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
            String familyName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
            String suffix = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX));
        }

我真正看到的唯一区别是我在 where 参数中添加了 mime-type,并且我检索数据的方式略有不同。

于 2013-02-28T20:36:37.653 回答
0
/* Return all contacts and show each contact data in android monitor    console as debug info. */    private List<ContactDTO> getAllContacts()    {
       List<ContactDTO> ret = new ArrayList<ContactDTO>();
       // Get all raw contacts id list.
       List<Integer> rawContactsIdList = getRawContactsIdList();
       int contactListSize = rawContactsIdList.size();
       ContentResolver contentResolver = getContentResolver();
       // Loop in the raw contacts list.
       for(int i=0;i<contactListSize;i++)
       {
           // Get the raw contact id.
           Integer rawContactId = rawContactsIdList.get(i);
           Log.d(TAG_ANDROID_CONTACTS, "raw contact id : " + rawContactId.intValue());
           // Data content uri (access data table. )
           Uri dataContentUri = ContactsContract.Data.CONTENT_URI;
           // Build query columns name array.
           List<String> queryColumnList = new ArrayList<String>();
           // ContactsContract.Data.CONTACT_ID = "contact_id";
           queryColumnList.add(ContactsContract.Data.CONTACT_ID);
           // ContactsContract.Data.MIMETYPE = "mimetype";
           queryColumnList.add(ContactsContract.Data.MIMETYPE);
           queryColumnList.add(ContactsContract.Data.DATA1);
           queryColumnList.add(ContactsContract.Data.DATA2);
           queryColumnList.add(ContactsContract.Data.DATA3);
           queryColumnList.add(ContactsContract.Data.DATA4);
           queryColumnList.add(ContactsContract.Data.DATA5);
           queryColumnList.add(ContactsContract.Data.DATA6);
           queryColumnList.add(ContactsContract.Data.DATA7);
           queryColumnList.add(ContactsContract.Data.DATA8);
           queryColumnList.add(ContactsContract.Data.DATA9);
           queryColumnList.add(ContactsContract.Data.DATA10);
           queryColumnList.add(ContactsContract.Data.DATA11);
           queryColumnList.add(ContactsContract.Data.DATA12);
           queryColumnList.add(ContactsContract.Data.DATA13);
           queryColumnList.add(ContactsContract.Data.DATA14);
           queryColumnList.add(ContactsContract.Data.DATA15);
           // Translate column name list to array.
           String queryColumnArr[] = queryColumnList.toArray(new String[queryColumnList.size()]);
           // Build query condition string. Query rows by contact id.
           StringBuffer whereClauseBuf = new StringBuffer();
           whereClauseBuf.append(ContactsContract.Data.RAW_CONTACT_ID);
           whereClauseBuf.append("=");
           whereClauseBuf.append(rawContactId);
           // Query data table and return related contact data.
           Cursor cursor = contentResolver.query(dataContentUri, queryColumnArr, whereClauseBuf.toString(), null, null);
           /* If this cursor return database table row data.
              If do not check cursor.getCount() then it will throw error
              android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0.
              */
           if(cursor!=null && cursor.getCount() > 0)
           {
               StringBuffer lineBuf = new StringBuffer();
               cursor.moveToFirst();
               lineBuf.append("Raw Contact Id : ");
               lineBuf.append(rawContactId);
               long contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID));
               lineBuf.append(" , Contact Id : ");
               lineBuf.append(contactId);
               do{
                   // First get mimetype column value.
                   String mimeType = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
                   lineBuf.append(" \r\n , MimeType : ");
                   lineBuf.append(mimeType);
                   List<String> dataValueList = getColumnValueByMimetype(cursor, mimeType);
                   int dataValueListSize = dataValueList.size();
                   for(int j=0;j < dataValueListSize;j++)
                   {
                       String dataValue = dataValueList.get(j);
                       lineBuf.append(" , ");
                       lineBuf.append(dataValue);
                   }
               }while(cursor.moveToNext());
               Log.d(TAG_ANDROID_CONTACTS, lineBuf.toString());
           }
           Log.d(TAG_ANDROID_CONTACTS, "=========================================================================");
       }
       return ret;    }
于 2021-10-09T06:27:12.313 回答
0

Android 通讯录刚刚重生!使用Contacts, Reborn API,原始问题的答案,

我在我的应用程序中使用以下代码来检索姓名和姓氏表格选择的联系人......它不能给我姓名和家庭并返回 null。有谁能够帮我?

是以下...

在 Java 中,

public void printContactFullName(Context context, Long contactId) {
    Contacts contactsApi = ContactsFactory.create(context);
    Contact contact = contactsApi
            .query()
            .include(Fields.Name.getAll())
            .where(WhereKt.equalTo(Fields.Contact.Id, contactId))
            .find()
            .get(0);

    // Assuming this contact only have 1 RawContact...
    Name name = ContactDataKt.nameList(contact).get(0);
    Log.d(
            "Contact",
            "Prefix: " + name.getPrefix() +
            ", Given name: " + name.getGivenName() +
            ", Middle name: " + name.getMiddleName() +
            ", Family name: " + name.getFamilyName() +
            ", Suffix: " + name.getSuffix()
    );
}

在科特林,

fun printContactFullName(context: Context, contactId: Long) {
    Contacts(context)
        .query()
        .include(Fields.Name.all)
        .where(Fields.Contact.Id equalTo contactId)
        .find()
        .first().let { contact ->
            // Assuming this contact only have 1 RawContact...
            val name = contact.names().first()
            Log.d(
                "Contact",
                """
                    Prefix: ${name.prefix}
                    Given name: ${name.givenName}
                    Middle name: ${name.middleName}
                    Family name: ${name.familyName}
                    Suffix: ${name.suffix}
                """.trimIndent()
            )
        }
}

但是请记住,并非所有联系人都必须具有专有名称。用户可以创建没有姓名的联系人。这是本机联系人应用程序中的行为。

您可以使用Contacts, Reborn API做更多的事情。您甚至可以构建自己的成熟联系人应用程序,该应用程序可以与几乎所有 Android 副本附带的应用程序并驾齐驱。

于 2021-10-09T01:03:09.090 回答