How to update existing contact?

前端 未结 6 1894
闹比i
闹比i 2020-12-15 05:26

I have one existing contact, I need to add a work address to that existing contact. I am using the following code, but it\'s not working.

String selectPhone          


        
6条回答
  •  北海茫月
    2020-12-15 06:07

    Finally I found the appropriate solution..Much thanks to this How to modify existing Contact

    The secret is that you have to pass two values for .withSelection as shown below:

    .withSelection(Data.RAW_CONTACT_ID + " = ?", new String[] {String.valueOf(id)})
    .withSelection(Data._ID + " = ?", new String[] {mDataId})
    

    where by Data._ID value mDataId is obtained this way:

    Cursor mDataCursor = this.context.getContentResolver().query(
                            Data.CONTENT_URI,
                            null,
                            Data.RAW_CONTACT_ID + " = ? AND " + Data.MIMETYPE + " = ?",
                            new String[] { String.valueOf(id), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE},
                            null);
    
                    if(mDataCursor.getCount() > 0) {
                        mDataCursor.moveToFirst();
                        mDataId = getCursorString(mDataCursor, Data._ID);
                        MLog.v("Data", "Found data item with MIMETYPE");                            
                        mDataCursor.close();
    
                    } else {
                        MLog.v("Data", "Data doesn't contain MIMETYPE");
                        result = ERROR;
                        mDataCursor.close();
                    } 
    

    And getCursorString method is something like:

    private static String getCursorString(Cursor cursor, String columnName) {
            int index = cursor.getColumnIndex(columnName);
            if(index != -1) return cursor.getString(index);
            return null;
        }
    

    This and only this is the trick..

提交回复
热议问题