That's right. DATA1 is used for the most important data element by convention.
Email has an unfortunate alias "DATA" for DATA1. In 2.1 we will also have Email.ADDRESS mapping to DATA1. The DATA1 column is indexed, so doing searches on that column in combination with a MIMETYPE constraint will be the most efficient. Cheers, - Dmitri On Mon, Dec 21, 2009 at 10:07 AM, Molee <[email protected]> wrote: > > for content of email and note, by convention, you can put then into > column data1 directly > ContactsContract.DataColumns.data1 > > i found that there is no object in > ContactsContract.CommonDataKinds.Email > representing data1 hence i just put data1 directly and it works > > ContactsContract.CommonDataKinds.Phone.NUMBER = data1 as well > > hope it helps > > > > On 11月17日, 下午3時13分, EMMNS <[email protected]> wrote: > > 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 <[email protected]> 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. <[email protected]> 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 > [email protected] > > > > To unsubscribe from this group, send email to > > > > [email protected]<android-developers%[email protected]> > <android-developers%[email protected]<android-developers%[email protected]> > > > > > > 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 [email protected] > To unsubscribe from this group, send email to > [email protected]<android-developers%[email protected]> > 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

