2012-10-21 278 views
3

我正在構建需要從Android聯繫人中選擇聯繫人的應用程序。我這樣做,我在我的應用程序中使用以下代碼檢索名稱和姓氏表單所選聯繫人(在onActivityResult中)。從Android聯繫人獲取聯繫人姓名和姓氏

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。有誰能夠幫助我?

回答

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

唯一的區別我真的看到的是我加的mime-type到上面的參數和我略有不同檢索數據。

相關問題