views:

105

answers:

1

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});

        }

        }
A: 

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

Thorstenvv
Excelent this got me where I needed to go.