Android游标查询中复杂的WHERE条件
我想查询联系人数据并检索联系人姓名和电话号码,条件如下:如果联系人有手机号码,则选择该号码,否则选择该联系人拥有的任何号码/第一个号码。是否可以在游标查询中制定此条件,或者我是否必须在自定义游标适配器中执行此操作?
这是我目前拥有的代码。它工作正常,但它会检索所有联系人的所有号码,因此,如果一个人有多个联系人,我会得到重复的姓名。
private String WHERE_CONDITION = ContactsContract.Data.MIMETYPE + " = '" +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'";
private String[] PROJECTION = {ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.DATA1, ContactsContract.Data._ID };
private String SORT_ORDER = ContactsContract.Data.DISPLAY_NAME;
cursor = this.getContentResolver().query(
ContactsContract.Data.CONTENT_URI, PROJECTION, WHERE_CONDITION, null, SORT_ORDER);
非常感谢任何帮助!
I want to query the Contacts data and retrieve a contact name and a phone number with the following condition: if a contact has a mobile number then pick that number, else pick any number/first number the contact has. Is it possible to formulate this condition in a cursor query or would I have to do it within a custom cursor adapter?
This is the code I have at the moment. It works fine but it retrieves all numbers for all contacts, therefore I get duplicate names if a person has more than one contact.
private String WHERE_CONDITION = ContactsContract.Data.MIMETYPE + " = '" +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'";
private String[] PROJECTION = {ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.DATA1, ContactsContract.Data._ID };
private String SORT_ORDER = ContactsContract.Data.DISPLAY_NAME;
cursor = this.getContentResolver().query(
ContactsContract.Data.CONTENT_URI, PROJECTION, WHERE_CONDITION, null, SORT_ORDER);
Any help is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果把这个逻辑放在Java中会容易得多、简单得多。只需检索所有号码并选择您需要的号码即可。通常,您最多有 3 个数字,也可能是 5 个。无论如何,开销都会非常低。
It would be much easier and simpler to put this logic in Java. Just retrieve all numbers and select the one you need. Typically you will have at most 3 numbers, may be 5. In any case overhead will be very low.