以下查询在我的设备 (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
可以从接口(interface) android.provider.ContactsContract.ContactsColumns 访问 HAS_PHONE_NUMBER
更奇怪的是,这个查询有效:
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
相同的隐式连接!我哪里错了?
最佳答案
您遇到的主要问题是 ContactsContract.CommonDataKinds.Phone.CONTENT_URI
自动 过滤 has_phone_number=1
。所以你可以保证从 Phone.CONTENT_URI
出来的所有东西都有一个电话号码。如果您想限制电话号码的类型,您可以通过检查 Phone.TYPE
是否是 Phone.TYPE_MOBILE
或 Phone.TYPE_WORK_MOBILE
之一(例如)来进一步过滤。
供引用(请注意,has_phone_number
URI 中没有 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
关于Android API 8 , 10 ContactsContract.Data.HAS_PHONE_NUMBER 没有这样的列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11812686/