开发者

access to contact data

开发者 https://www.devze.com 2023-01-15 03:34 出处:网络
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 t

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.StructuredNam开发者_运维技巧e.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});

        }

        }


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:

query(ContactsContract.Data.CONTENT_URI,  Data.MIMETYPE + "=?", 
    new String[]{StructuredName.CONTENT_ITEM_TYPE}, null, null);

See the documentation for ContactsContract.Data

0

精彩评论

暂无评论...
验证码 换一张
取 消