访问 Android 联系人所有详细信息的最佳方式
我正在编写一个应用程序来与我的服务器同步联系人详细信息。为此,我需要多次使用内容提供程序查询联系人,因为所有数据并不存在于一张表中。例如,通过使用联系人 ID,我必须分别查询每个联系人的电话、电子邮件和地址表,我觉得这效率不高。如果有人能指出我在单个查询中获取所有联系方式的方法,那将非常有帮助。提前致谢 :)
I am writing an application to sync contact details with my server. For this I need to query Contacts using content provider multiple times as all the data is not present in one table. For example by using contact id, I have to query separately to phone, email and address tables for each contact which I feel is not much efficient. It would be really helpful if someone could point me out ways to get all the contact details in a single query. Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有 rawContactId,则不需要多次查询。您可以使用 Data.CONTENT_URI 作为 uri 进行单个查询,并在 rawContactId 上进行选择。
您必须使用结果光标循环才能读取信息。要知道在哪一列中,您需要查看数据表中的给定行,您需要检查 MIMETYPE
编辑
请考虑我没有检查代码:我这里没有编译器。无论如何我希望它会有用
If you have the rawContactId you don't need multiple query. You can have a single query with Data.CONTENT_URI as uri and with a selection on your rawContactId.
The you have to loop with the resulting cursor to read the info. To know in wich column you need to see for a given row in the Data table you need to check the MIMETYPE
EDIT
Please consider that I did not check the code: I do not have a compiler here. I hope it will be useful anyway
Data 是一个通用表,可以容纳任何类型的联系人数据。
给定行中存储的数据类型由该行的 MIMETYPE 值指定,该值确定通用列 DATA1 到 DATA15 的含义。
例如,如果数据类型为 Phone.CONTENT_ITEM_TYPE,则列 DATA1 存储电话号码,但如果数据类型为 Email.CONTENT_ITEM_TYPE,则 DATA1 存储电子邮件地址。同步适配器和应用程序可以引入自己的数据类型。
Data is a generic table that can hold any kind of contact data.
The kind of data stored in a given row is specified by the row's MIMETYPE value, which determines the meaning of the generic columns DATA1 through DATA15.
For example, if the data kind is Phone.CONTENT_ITEM_TYPE, then the column DATA1 stores the phone number, but if the data kind is Email.CONTENT_ITEM_TYPE, then DATA1 stores the email address. Sync adapters and applications can introduce their own data kinds.