2011-12-08 65 views
8

我目前用的Android通訊錄內容提供商合作,目前還沒有問題訪問聯繫人全顯示名稱使用下面的代碼:獲取聯繫人的姓氏和名字而不是單個顯示名稱?

String[] PROJECTION = new String[] { 
     ContactsContract.Contacts._ID, 
     ContactsContract.Contacts.DISPLAY_NAME, 
     ContactsContract.Contacts.HAS_PHONE_NUMBER, 
     }; 
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 

String SELECTION = "LOWER(" + ContactsContract.Contacts.DISPLAY_NAME + ")" 
    + " LIKE '" + constraint + "%' " + "and " + 
     ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'"; 

Cursor cur = managedQuery(ContactsContract.Contacts.CONTENT_URI, 
       PROJECTION, SELECTION, null, sortOrder); 

不過,我希望能夠以姓氏和名字得到兩個觸頭另外,我試圖使用StructuredName試圖獲得這個,但我似乎無法得到它的工作。

任何人都可以點我在正確的方向上如何正確使用StructuredName得到名稱分成第一和最後?

UPDATE:

繼hovanessyan的意見,我已經嘗試以下操作:

String[] PROJECTION = new String[] { 
      ContactsContract.Data._ID, 
      ContactsContract.Data.DISPLAY_NAME, 
      ContactsContract.Data.HAS_PHONE_NUMBER, 
    }; 

    String SELECTION = ContactsContract.CommonDataKinds.StructuredName.IN_VISIBLE_GROUP + " = '1'"; 

    String sortOrder = ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 

    Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, null, sortOrder); 

    int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 
    int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME); 
    int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME); 

    while (cursor.moveToNext()) { 
     String given = cursor.getString(indexGivenName); 
     String family = cursor.getString(indexFamilyName); 
     String display = cursor.getString(indexDisplayName); 
     Log.e("XXX", "Name: | " + given + " | " + family + " | " + display); 
    } 

但是使用投影導致系統崩潰如下:

12-08 16:52:01.575: E/AndroidRuntime(7912): Caused by: java.lang.IllegalArgumentException: Invalid column has_phone_number 

如果我刪除投影我得到所有結果打印出來,但很多都包含NULL。

例如:

12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null 
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null 
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null 
12-08 16:56:14.055: E/XXX(8155): Name: | null | null | null 

因此,誰能看到我在我的投影不起作用做錯了什麼?

進一步更新:

我已經整理出來的問題與我的投影,但現在我已經在數據內容提供商與所有空數據提供我回來,在我的代碼導致空指針異常的問題。

從ContactsContract.Contacts光標數給我回115例如但使用的數據表使用相同的參數給我回464,這在我的應用程序造成巨大的問題。

任何人有任何想法,這是爲什麼?

回答

13

看看ContactsContract.CommonDataKinds.StructuredName類。你把所有的需要的列在那裏,你也許可以這樣做:

Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, other_query_params_for_filtering); 

int indexGivenName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 
int indexFamilyName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME); 
int indexDisplayName = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME); 

while (cursor.moveToNext()) { 
    String given = cursor.getString(indexGivenName); 
    String family = cursor.getString(indexFamilyName); 
    String display = cursor.getString(indexDisplayName); 
} 
+0

它有許多工作要做在FilterQuery中,雖然這似乎是導致問題,我需要能夠獲得ID,DisplayName和HasPhoneNumber列,其中我的字符串約束等於給定或系列名稱。所以我需要一個managedQuery,這似乎是一些問題。 –

+0

你必須使用通用表中的數據,以實現這一目標:http://developer.android.com/reference/android/provider/ContactsContract.Data.html – hovanessyan

+0

我已經嘗試使用通用數據表來獲取信息和遇到了一些問題,我編輯了我的問題,以防您可以進一步幫助,謝謝。 –

2

這裏是從ContactsContract.Data表中獲取用戶數據的一般功能:

Map<String, String> result = new HashMap<>(); 
Cursor cursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.CONTACT_ID + "='" + YOUR_CONTACT_ID + "'", null, null); 
if (cursor != null) { 
    while (cursor.moveToNext()) { 
     String mime = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE)); 
     switch (mime) { 
      case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE: 
       result.put(FIRST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME))); 
       result.put(LAST_NAME, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))); 
       break; 
      case ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE: 
       result.put(CITY, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY))); 
       result.put(STREET, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET))); 
       result.put(ZIP, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE))); 
       break; 
      case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE: 
       if (ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE == cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))) { 
        result.put(MOBILE, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 
       } 
       break; 
     } 
    } 
    cursor.close(); 
} 
return result; 
2
 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();