访问联系数据
唉,我的电话簿中有大约 500 个联系人,由于某种原因,在将它们与 Thunderbird 同步后,显示名称是随机的最后、第一个...第一个最后一个。所以我想我应该把一个快速的小部件放在一起,以重新完成我的显示名称到最后,第一个。我使用的代码如下,但是我没有得到最后/第一个值。游标中的键存在(data1,data2),但值分别为“1”和null。有什么想法吗?
Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext() != false) {
String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String fname = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String lname = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
if (lname != null && lname.length() > 0) {
String sDName = lname + "," + fname;
ContentValues values = new ContentValues();
values.put(ContactsContract.Contacts.DISPLAY_NAME, sDName);
getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Contacts._ID+"=", new String[] {id});
}
}
Alas I have about 500 contacts in my phone book and for some reason after synch'ing them with thunderbird the display name is random last, first...first last. So I thought I would put a quick widget together to just re-do al my display names to last, first. The code I use is below, however I am not getting last / first values. The keys in the cursor exist (data1, data2), but the values were "1" and null respectively. Any Ideas?
Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext() != false) {
String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String fname = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
String lname = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
if (lname != null && lname.length() > 0) {
String sDName = lname + "," + fname;
ContentValues values = new ContentValues();
values.put(ContactsContract.Contacts.DISPLAY_NAME, sDName);
getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Contacts._ID+"=", new String[] {id});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ContactsContract.Data 在共享表中包含所有各种类型的信息,如邮政地址、电话号码、电子邮件、网站、照片等。
您必须过滤掉包含您感兴趣的信息的行。
在查询中,添加 WHERE 子句:
请参阅 ContactsContract.Data
ContactsContract.Data contains all various types of information like postal address, phone numbers, e-mail, web sites, photes etc. in a shared table.
You have to filter out the rows that contain the information you are interested in.
In the query, add a WHERE clause:
See the documentation for ContactsContract.Data