4

以下查询在我的设备(API 15)上运行良好。它在我运行 API 8 或 API 10 的模拟器中不起作用。

这是查询:

Cursor contactsCur = getContentResolver().query(
  ContactsContract.Data.CONTENT_URI,
  new String[] { ContactsContract.Data._ID,
    ContactsContract.Data.CONTACT_ID,
    ContactsContract.Data.DISPLAY_NAME,
    ContactsContract.CommonDataKinds.Organization.COMPANY,
    ContactsContract.CommonDataKinds.Phone.TYPE,
    ContactsContract.Data.LOOKUP_KEY,
    ContactsContract.CommonDataKinds.Phone.NUMBER,
    Contacts.Data.MIMETYPE,
    ContactsContract.CommonDataKinds.Phone.MIMETYPE,
    ContactsContract.CommonDataKinds.StructuredPostal.CITY,
    ContactsContract.CommonDataKinds.StructuredPostal.STREET,
    ContactsContract.CommonDataKinds.StructuredPostal.REGION,
    ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE},
  ContactsContract.Data.HAS_PHONE_NUMBER + " >? ",
  new String[] { "0" },
  ContactsContract.Data.DISPLAY_NAME + " COLLATE NOCASE ASC");

这是它抛出的异常: android.database.sqlite.SQLiteException: no such column: has_phone_number: , while compiling: SELECT _id, contact_id, display_name, data1, data2, lookup, data1, mimetype, mimetype, data7, data4, data8, data9 FROM view_data_restricted data WHERE (1) AND (has_phone_number >? ) ORDER BY display_name COLLATE NOCASE ASC

奇怪的是,以下代码也不起作用:

Cursor contactsCur = getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
    new String[] { ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER},
    ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + " >? ",
    new String[] { "0" },
    null);

它产生:java.lang.IllegalArgumentException: Invalid column has_phone_number

http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html声明可以ContactsContract.CommonDataKinds.Phone访问HAS_PHONE_NUMBERFrom 接口 android.provider.ContactsContract.ContactsColumns

更奇怪的是,这个查询有效:

Cursor contactsCur = getContentResolver().query(
    ContactsContract.Data.CONTENT_URI,
    new String[] { ContactsContract.Data.LOOKUP_KEY},
    null,
    null,
    null);

但是这个没有:

Cursor contactsCur = getContentResolver().query(
    ContactsContract.Data.CONTENT_URI,
    new String[] { ContactsContract.Data.HAS_PHONE_NUMBER},
    null,
    null,
    null);

ContactsContract.Data.LOOKUP_KEY来自与ContactsContract.Data.HAS_PHONE_NUMBER!相同的隐式连接

我哪里错了?

4

1 回答 1

5

您遇到的主要问题是ContactsContract.CommonDataKinds.Phone.CONTENT_URI 自动过滤has_phone_number=1. Phone.CONTENT_URI 所以你可以保证所有出来的东西都有一个电话号码。Phone.TYPE然后,您可以通过检查是否是其中之一Phone.TYPE_MOBILEPhone.TYPE_WORK_MOBILE(例如)如果您想限制它拥有的电话号码类型,可以进一步过滤。


供参考(请注意URIhas_phone_number中不存在):data/phones

Columns for content://com.android.contacts/contacts:
- times_contacted
- contact_status
- custom_ringtone
- has_phone_number
- phonetic_name
- phonetic_name_style
- contact_status_label
- lookup
- contact_status_icon
- last_time_contacted
- display_name
- sort_key_alt
- in_visible_group
- _id
- starred
- sort_key
- display_name_alt
- contact_presence
- display_name_source
- contact_status_res_package
- contact_status_ts
- photo_id
- send_to_voicemail

和:

Columns for content://com.android.contacts/data/phones:
- data_version
- phonetic_name
- phonetic_name_style
- contact_id
- lookup
- data12
- data11
- data10
- mimetype
- data15
- data14
- data13
- display_name_source
- data_sync1
- data_sync3
- data_sync2
- data_sync4
- account_type
- custom_ringtone
- status
- data1
- data4
- data5
- data2
- data3
- data8
- data9
- group_sourceid
- data6
- account_name
- data7
- display_name
- in_visible_group
- display_name_alt
- contact_status_res_package
- is_primary
- contact_status_ts
- raw_contact_id
- times_contacted
- contact_status
- status_res_package
- status_icon
- contact_status_icon
- mode
- version
- last_time_contacted
- res_package
- _id
- name_verified
- status_ts
- dirty
- is_super_primary
- photo_id
- send_to_voicemail
- name_raw_contact_id
- contact_status_label
- status_label
- sort_key_alt
- starred
- sort_key
- contact_presence
- sourceid
于 2013-10-21T18:08:20.893 回答