gpt4 book ai didi

java - Android 通讯录 - 更新说明

转载 作者:太空狗 更新时间:2023-10-29 13:40:24 29 4
gpt4 key购买 nike

我正在使用新的 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()); }
}

最佳答案

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

试试这个代码...

关于java - Android 通讯录 - 更新说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6506260/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com