Hi all,

thanks for your posts, that got me started and going on the new sdk
2.0, but still facing some problems adding contacts.

let's say i have an array of String has the contacts info

String[] c = new String[] {"First name","Last name","mobile number",
"home number", "work number", "fax number", "email address", "some
notes"}

please help me adding these to a single contact, I read the code you
guys posted and helped me to start,

it adds the contact info
but can't add the email or the notes

also it seems it adds a new contact on every field ??

here is my code:

<code>

ContentValues values = new ContentValues();

values.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, c[1]+
" " + c[0]);

Uri rawContactUri = getContentResolver().insert(
RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);

values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.MIMETYPE,
StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, c[1] + " " + c[0]);
getContentResolver().insert(ContactsContract.Data.CONTENT_URI,
values);

Uri mobileUri = null;
Uri homeUri = null;
Uri emailUri = null;
Uri workUri = null;
Uri faxUri = null;
Uri noteUri = null;

mobileUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
values.clear();
values.put(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
values.put(ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY,
1);
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, c[2]);
getContentResolver().insert(mobileUri, values);

homeUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
values.clear();
values.put(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_HOME);
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, c[3]);
getContentResolver().insert(homeUri, values);

workUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
values.clear();
values.put(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, c[5]);
getContentResolver().insert(workUri, values);

faxUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
values.clear();
values.put(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME);
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, c[6]);
getContentResolver().insert(faxUri, values);


emailUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);

values.clear();
values.put(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_OTHER);
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Email.DISPLAY_NAME, c[4]);
getContentResolver().insert(emailUri, values);

noteUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
values.clear();
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Note.NOTE, c[7]);
getContentResolver().insert(noteUri, values);


</code>


Thank you for your help in advance.










On Nov 2, 5:24 pm, Dmitri Plotnikov <dplotni...@google.com> wrote:
> You can always delegate contact creation to the Contacts app using the
> ContactsContract.Intents.UI.Insert intent with extras. This will show the
> edit UI.
>
> If you want to explicitly create the contact by yourself, that's now a bit
> tricky because Android 2.0 support multiple accounts.
>
> First of all, you will need to figure out which account you want to create
> the contact in. Get a list of all available accounts from AccountManager:
>
> AccountManager am = AccountManager.get(getContext());
> Account[] accounts = am.getAccounts();
>
> Also, get a list of all sync adapters and find the ones that support
> contacts:
>
> SyncAdapterType[] syncs
> = ContentResolver.getContentService().getSyncAdapterTypes();
>
> for (SyncAdapterType sync : syncs) {
>      if (ContactsContract.AUTHORITY.equals(sync.authority) &&
> sync.supportsUploading()) {
>           contactAccountTypes.add(sync.accountType);
>      }
>
> }
>
> Now you have a list of all accounts and a list of account types that support
> contacts.  So here's your account list:
>
> for (Account acct: accounts) {
>    if (contactAccountTypes.contains(acct.type)) {
>       contactAccounts.add(account);
>    }
>
> }
>
> If the contactAccounts list contains nothing - use accountType = null and
> accountName = null
> If it contains exactly one account, use it.
> If it contains multiple accounts, build a dialog and ask the user which
> account to use.
>
> From here on it gets easier.
>
> Let's start with a more traditional method.  Insert a raw contact first:
>
> ContentValues values = new ContentValues();
> values.put(RawContacts.ACCOUNT_TYPE, accountType);
> values.put(RawContacts.ACCOUNT_NAME, accountName);
> Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI,
> values);
> long rawContactId = ContentUris.parseId(rawContactUri);
>
> Then insert the name:
>
> values.clear();
> values.put(Data.RAW_CONTACT_ID, rawContactId);
> values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
> values.put(StructuredName.DISPLAY_NAME, "Some Body");
> getContentResolver().insert(Data.CONTENT_URI, values);
>
> You are done.
>
> Now here's a much better way to do the same.  Use the
> new ContentProviderOperation API, which will ensure that the raw contact and
> its name are inserted at the same time.
>
> ArrayList<ContentProviderOperation> ops = Lists.newArrayList();
> 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, 0)
>         .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
>         .withValue(StructuredName.DISPLAY_NAME, "Some Body")
>         .build());
>
> getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
>
> I hope this helps.
> - Dmitri
>
> On Mon, Nov 2, 2009 at 4:23 PM, jak. <koda...@gmail.com> wrote:
> > Hello,
>
> > I'm currently working on porting our Android app to 2.0 but I'm having
> > a rather hard time figuring out what is required to interact with the
> > new Contacts API.
> > I'm using reflection to decide whether the new API is available, if it
> > is I attempt to use the new features, otherwise I fall back to our old
> > methods.
>
> > However, I'm having a hard time finding analogs to the old
> > functionality in the new API.
> > For example in the past I was adding contacts to the database from an
> > external text source by creating a ContentValues object, filling it
> > with information on the contact and then adding it with a call to:
> > Contacts.People.createPersonInMyContactsGroup(...);
>
> > i.e.:
> > ...
>
> > ContentValues personValues = new ContentValues();
> > personValues.put(Contacts.People.NAME, "Some Body");
> > Uri personUri = Contacts.People.createPersonInMyContactsGroup
> > (curContext().getContentResolver(), personValues);
>
> > ...
> > How can I achieve the same goal in the new API?
>
> > I appreciate all the hard work going into improving the APIs but I
> > must admit I'm a bit frustrated by the lack of documentation and
> > examples.
> > I realize we have plenty of Javadocs on developer.android.com to
> > reference, but those only really show us what the new interfaces are.
> > I'm having a really hard time finding any discussion in terms of how
> > the new API is intended to be used.
>
> > Any help would be greatly appreciated!
> > Thanks!
>
> > --
> > You received this message because you are subscribed to theGoogle
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com>
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to