Android联系人 - 更新说明

时间:2011-06-28 12:18:01

标签: java android contacts

我使用新的API来插入/更新手机上的联系人,我正在使用此方法更新它们。

String selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + Note.CONTENT_ITEM_TYPE + "'" + " AND " + Email.TYPE + "=?";
    String[] selectArgs2 = new String[]{contactId, String.valueOf(Email.TYPE_OTHER)}; 
    operationList.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
            .withSelection(selectArgs1, selectArgs2)
            .withValue(Note.Note, "hello@hi.com")
            .build());

除了我更新笔记时,这一切都正常。没有Note.Type_1或类似的东西,所以我不知道在selectArgs1和selectArgs2中放入什么来代替Email.TYPEString.valueOf(Email.TYPE_OTHER)

任何帮助都会被贬低。谢谢! :)

[编辑]

public static void updateContact(Context c, int recordId, String key, String value) {
    context = c;
    if (database == null) database = new SavedContactsDatabase(context);
    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
    ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(Data.CONTENT_URI);
    String contactId = String.valueOf(recordId);
    String selectArgs1 = "";
    String[] selectArgs2 = null;

    String type = "";
    String subkey = "";
    if (key.contains(":")) {
        String[] split = key.split(":");
        key = split[0];
        type = split[1];
        if (split.length == 3) subkey = split[2];
    }

    if (key.equals("Email")) {
        if (type.equals("Home")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Email.TYPE_HOME)};
        } else if (type.equals("Work")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Email.TYPE_WORK)};
        } else if (type.equals("Mobile")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Email.TYPE_MOBILE)};
        }
        selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + Email.CONTENT_ITEM_TYPE + "'" + " AND " + Email.TYPE + "=?";
        builder.withSelection(selectArgs1, selectArgs2);
        builder.withValue(Email.DATA1, value);
    }

    if (key.equals("Nickname")) {
        if (type.equals("Default")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Nickname.TYPE_DEFAULT)};
        } else if (type.equals("Other Names")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Nickname.TYPE_OTHER_NAME)};
        } else if (type.equals("Maiden Name")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Nickname.TYPE_MAINDEN_NAME)};
        } else if (type.equals("Short Name")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Nickname.TYPE_SHORT_NAME)};
        } else if (type.equals("Initials")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Nickname.TYPE_INITIALS)};
        }
        selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + Nickname.CONTENT_ITEM_TYPE + "'" + " AND " + Nickname.TYPE + "=?";
        builder.withSelection(selectArgs1, selectArgs2);
        builder.withValue(Nickname.NAME, value);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////
    if (key.equals("Note")) {
        selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + Note.CONTENT_ITEM_TYPE + "'";
        selectArgs2 = new String[]{contactId, "vnd.android.cursor.item/note"}; 
        builder.withSelection(selectArgs1, selectArgs2);
        builder.withValue(Note.NOTE, "hello@hi.com");

    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////

    if (key.equals("Organization")) {
        String data = "";
        if (type.equals("Company")) {
            data = Organization.COMPANY;
        } else if (type.equals("Title")) {
            data = Organization.TITLE;
        } else if (type.equals("Department")) {
            data = Organization.DEPARTMENT;
        } else if (type.equals("Job Descripiton")) {
            data = Organization.JOB_DESCRIPTION;
        } else if (type.equals("Symbol")) {
            data = Organization.SYMBOL;
        } else if (type.equals("Phonetic Name")) {
            data = Organization.PHONETIC_NAME;
        } else if (type.equals("Office Location")) {
            data = Organization.OFFICE_LOCATION;
        }
        selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + Organization.CONTENT_ITEM_TYPE + "'" + " AND " + Organization.TYPE + "=?";
        selectArgs2 = new String[]{contactId, String.valueOf(Organization.TYPE_WORK)};
        builder.withSelection(selectArgs1, selectArgs2);
        builder.withValue(data, value);
    }

    if (key.equals("Phone")) {
        if (type.equals("Home")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_HOME)};
        } else if (type.equals("Mobile")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_MOBILE)};
        } else if (type.equals("Work")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_WORK)};
        } else if (type.equals("Fax Work")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_FAX_WORK)};
        } else if (type.equals("Fax Home")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_FAX_HOME)};
        } else if (type.equals("Pager")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_PAGER)};
        } else if (type.equals("Other")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_OTHER)};
        } else if (type.equals("Callback")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_CALLBACK)};
        } else if (type.equals("Car")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_CAR)};
        } else if (type.equals("Company Main")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_COMPANY_MAIN)};
        } else if (type.equals("ISDN")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_ISDN)};
        } else if (type.equals("Main")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_MAIN)};
        } else if (type.equals("Other Fax")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_OTHER_FAX)};
        } else if (type.equals("Radio")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_RADIO)};
        } else if (type.equals("Telex")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_TELEX)};
        } else if (type.equals("TTY TDD")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_TTY_TDD)};
        } else if (type.equals("Work Mobile")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_WORK_MOBILE)};
        } else if (type.equals("Work Pager")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_WORK_PAGER)};
        } else if (type.equals("Assistant")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_ASSISTANT)};
        } else if (type.equals("MMS")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Phone.TYPE_MMS)};
        }
        selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Phone.TYPE + "=?";
        builder.withSelection(selectArgs1, selectArgs2);
        builder.withValue(Phone.NUMBER, value);
    }

    //TODO Relation?

    //////////////////////////////////////////////////////////////////////////////////////
    if (key.equals("Name")) {
        String data = "";
        if (type.equals("Display Name")) {
            data = StructuredName.DISPLAY_NAME;
        } else if (type.equals("Given Name")) {
            data = StructuredName.GIVEN_NAME;
        } else if (type.equals("Family Name")) {
            data = StructuredName.FAMILY_NAME;
        } else if (type.equals("Prefix")) {
            data = StructuredName.PREFIX;
        } else if (type.equals("Middle Name")) {
            data = StructuredName.MIDDLE_NAME;
        } else if (type.equals("Suffix")) {
            data = StructuredName.SUFFIX;
        } else if (type.equals("Phonetic Given Name")) {
            data = StructuredName.PHONETIC_GIVEN_NAME;
        } else if (type.equals("Phonetic Middle Name")) {
            data = StructuredName.PHONETIC_MIDDLE_NAME;
        } else if (type.equals("Phonetic Family Name")) {
            data = StructuredName.PHONETIC_FAMILY_NAME;
        }
        selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + StructuredName.CONTENT_ITEM_TYPE + "'" + " AND " + StructuredName.MIMETYPE + "=?";
        selectArgs2 = new String[]{contactId, String.valueOf(StructuredName.MIMETYPE)};
        builder.withSelection(selectArgs1, selectArgs2);
        builder.withValue(data, value);
    }
    ////////////////////////////////////////////////////////////////////////////////////////

    if (key.equals("Address")) {
        String data = "";
        if (type.equals("Home")) {
            selectArgs2 = new String[]{contactId, String.valueOf(CommonDataKinds.StructuredPostal.TYPE_HOME)};
        } else if (type.equals("Work")) {
            selectArgs2 = new String[]{contactId, String.valueOf(CommonDataKinds.StructuredPostal.TYPE_WORK)};
        } else if (type.equals("Other")) {
            selectArgs2 = new String[]{contactId, String.valueOf(CommonDataKinds.StructuredPostal.TYPE_OTHER)};
        }
        if (subkey.equals("Street")) {
            data = CommonDataKinds.StructuredPostal.STREET;
        } else if (subkey.equals("PO Box")) {
            data = CommonDataKinds.StructuredPostal.POBOX;
        } else if (subkey.equals("Neighbourhood")) {
            data = CommonDataKinds.StructuredPostal.NEIGHBORHOOD;
        } else if (subkey.equals("City")) {
            data = CommonDataKinds.StructuredPostal.CITY;
        } else if (subkey.equals("Region")) {
            data = CommonDataKinds.StructuredPostal.REGION;
        } else if (subkey.equals("Postcode")) {
            data = CommonDataKinds.StructuredPostal.POSTCODE;
        } else if (subkey.equals("Country")) {
            data = CommonDataKinds.StructuredPostal.COUNTRY;
        }
        selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE + "'" + " AND " + CommonDataKinds.StructuredPostal.TYPE + "=?";
        builder.withSelection(selectArgs1, selectArgs2);
        builder.withValue(data, value);
    }

    if (key.equals("Website")) {
        if (type.equals("Homepage")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_HOMEPAGE)};
        } else if (type.equals("Blog")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_BLOG)};
        } else if (type.equals("Profile")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_PROFILE)};
        } else if (type.equals("Home")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_HOME)};
        } else if (type.equals("Work")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_WORK)};
        } else if (type.equals("FTP")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Website.TYPE_FTP)};
        }
        selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + Website.CONTENT_ITEM_TYPE + "'" + " AND " + Website.TYPE + "=?";
        builder.withSelection(selectArgs1, selectArgs2);
        builder.withValue(Website.URL, value);
    }

    if (key.equals("Event")) {
        if (type.equals("Anniversary")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Event.TYPE_ANNIVERSARY)};
        } else if (type.equals("Birthday")) {
            selectArgs2 = new String[]{contactId, String.valueOf(Event.TYPE_BIRTHDAY)};
        }
        selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + Event.CONTENT_ITEM_TYPE + "'" + " AND " + Event.TYPE + "=?";
        builder.withSelection(selectArgs1, selectArgs2);
        builder.withValue(Event.START_DATE, value);
    }





    /*
    operationList.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
            .withSelection(Data.RAW_CONTACT_ID + "=?", new String[] {sdf})
            .withValue(Email.DATA1, "work.hi.com")
            .withValue(Email.TYPE, Email.TYPE_WORK)
            .build());  
    */
    /*
    String selectPhone = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='"  + 
    Email.CONTENT_ITEM_TYPE + "'" + " AND " + Email.TYPE + "=?";
    String[] phoneArgs = new String[]{contactId, String.valueOf(Email.TYPE_OTHER)}; 
    operationList.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
            .withSelection(selectPhone, phoneArgs)
            .withValue(Email.DATA1, "hello@hi.comm")
            .build()); 
    */

    try {
        operationList.add(builder.build());
        ContentProviderResult[] res = context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
        if (res[0].count == 0) {
            //TODO Then Use the Insert Command Instead!
        }
    } catch (Exception e) { Log.d("fhksdsdj", e.toString()); }
}

1 个答案:

答案 0 :(得分:2)

String selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "= ? ";
    String[] selectArgs2 = new String[]{contactId, "vnd.android.cursor.item/note"}; 
    operationList.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
            .withSelection(selectArgs1, selectArgs2)
            .withValue(ContactsContract.CommonDataKinds.Note.NOTE, "hello@hi.com")
            .build());

试试这段代码......