开发者

Android write contacts [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to add new contacts in android

how can i add a name to the contacts? I have two spinners, one for the firstname and one for the lastname. The assigned variables are linkname1 and linkname2. When user pushes the OK button (these are all in a dialog), the name (linkname1 + " " + linkname2) should be added to the contacts. I can read the contacts but how do i write it?

Thanks

Update: I also tried this:

newname = linkname1 + " " + linkname2;
ContentValues values = new ContentValues();
vales.put(ContactsContract.Contacts.DISPLAY_NAME, newname);

and this:

StringBuffer strBuf = new StringBuffer();
strBuf.append(linkname1);
strBuf.append(" ");
strBuf.append(linkname2);
ContentValues values = new ContentValues();
values.put(ContactsContract.Contacts.DISPLAY_NAME, strBuf.toString());

But the new name is no开发者_高级运维t appearing in the contact list.


That content provider although will still work was replaced with Contacts Contracts in 2.2. The following code will work with new contacts contracts providers:

ArrayList<ContentProviderOperation> ops =
          new ArrayList<ContentProviderOperation>();
 ...
 int rawContactInsertIndex = ops.size();
 ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
          .withValue(RawContacts.ACCOUNT_TYPE, accountType)
          .withValue(RawContacts.ACCOUNT_NAME, accountName)
          .build());

 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
          .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
          .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
          .withValue(StructuredName.GIVEN_NAME, linkname1)
          .withValue(StructuredName.FAMILY_NAME, linkname2)
          .build());

 getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

The Account Name and Type you will have to query from the AccountManager service. Either by choosing the an account (e.g. Local or Google) or prompting the user.


Android how-to (create a new contact):

http://www.lacherstorfer.at/haris_blog/2008/03/android-howto-create-a-new-con.html

Be sure to look at the code edit in the comments, may help fix depending on version.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜