6

Alright, I'm a little new to the Android SDK, so forgive me if my question doesn't make sense or is very trivial. I'd like to add a custom field for contacts, that contains the contacts username on a website I'm doing this app for. And, with this custom field, I'd like to have the ability to click it (like "Send message" or "Call mobile") so that I can go to a specif Activity in my application, with a TextView set with the username that I just clicked on.

Sorry if that is a bit confusing, if you need anything else let me know!

Chiggins
  • 8,197
  • 22
  • 56
  • 81
  • possible duplicate of [How to add new field(s) to the contact?](http://stackoverflow.com/questions/2733589/how-to-add-new-fields-to-the-contact) – Pentium10 Jun 09 '10 at 21:53
  • possible duplicate of [How to programmatically assign a picture (Bitmap) to a contact ?](http://stackoverflow.com/questions/3076197/how-to-programmatically-assign-a-picture-bitmap-to-a-contact) – Pentium10 Jun 19 '10 at 19:16

3 Answers3

4

It's working! But I changed Data.CONTACT_ID to Data.RAW_CONTACT_ID here:

if (mod == 0) {
    values.put(Data.CONTACT_ID, this.getId());
    values.put(Data.MIMETYPE, clsContacts.FORMALITY_MIMETYPE);
    ctx.getContentResolver().insert(Data.CONTENT_URI, values);
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Natali
  • 2,934
  • 4
  • 39
  • 53
  • and also here:`int mod = ctx.getContentResolver().update( Data.CONTENT_URI, values, Data.CONTACT_ID + "=" + this.getId() + " AND " + Data.MIMETYPE + "= '" + clsContacts.FORMALITY_MIMETYPE + "'", null);` – Alexander A. Sharygin Feb 18 '13 at 18:44
3

You have to creat your own mime type for those.

Here is an example that saves a boolean as my custom mime type to the contacts. It uses the latest SDK 2.1

public static final String MIMETYPE_FORMALITY = "vnd.android.cursor.item/useformality";
public clsMyClass saveFormality() {
        try {
            ContentValues values = new ContentValues();
            values.put(Data.DATA1, this.getFormality() ? "1" : "0");
            int mod = ctx.getContentResolver().update(
                    Data.CONTENT_URI,
                    values,
                    Data.CONTACT_ID + "=" + this.getId() + " AND "
                            + Data.MIMETYPE + "= '"
                            + clsContacts.FORMALITY_MIMETYPE + "'", null);

            if (mod == 0) {
                values.put(Data.CONTACT_ID, this.getId());
                values.put(Data.MIMETYPE, clsContacts.FORMALITY_MIMETYPE);
                ctx.getContentResolver().insert(Data.CONTENT_URI, values);
            }
        } catch (Exception e) {
            Log.v(TAG(), "saveFormality failed");
        }
     return this;
    }

public boolean getFormality() {
     if (data.containsKey(FORMALITY)) {
        return data.getAsBoolean(FORMALITY);
    } else {
        // read formality
        Cursor c = readDataWithMimeType(clsContacts.MIMETYPE_FORMALITY, this.getId());
        if (c != null) {
            try {
                if (c.moveToFirst()) {
                    this.setFormality(c.getInt(0) == 1);
                    return (c.getInt(0) == 1);
                }
            } finally {
                c.close();
            }
        }
        return false;
    }

}
public clsMyClass setFormality(Boolean value) {
    data.remove(FORMALITY);
    data.put(FORMALITY, value);
    return this;
}

/**
 * Utility method to read data with mime type
 *
 * @param mimetype String representation of the mimetype used for this type
 *            of data
 * @param contactid String representation of the contact id
 * @return
 */
private Cursor readDataWithMimeType(String mimetype, String contactid) {
    return ctx.getContentResolver().query(
            Data.CONTENT_URI,
            new String[] {
                Data.DATA1
            },
            Data.RAW_CONTACT_ID + "=" + contactid + " AND " + Data.MIMETYPE + "= '" + mimetype
                    + "'", null, null);
}

Usage is

objContact.setFormality(true).saveFormality();
Pentium10
  • 204,586
  • 122
  • 423
  • 502
0

To add custom field you need to add custom mimetype in MIMETYPE table. But we dont have direct access to MIMETYPE table. so following can be done:

public static final String MIMETYPE="vnd.android.cursor.item/favsong";

         ContentValues values = new ContentValues();
    values.put(Data.RAW_CONTACT_ID, id);
    values.put(Data.MIMETYPE, MIMETYPE);
    values.put(Data.DATA1, "MyFavSong");
    Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);

what we have done is , we have created a custom MIMETYPE as string constant. Then using insert query we are inserting a new row in Data table having RAW_CONTACT_ID of a person we want to associate our custom field , in MIMETYPE column we put our own mimetype and in DATA1 column we put favourite song . here system internally add the new mimetype in MIMETYPE table and give it an ID and that ID is used in mimetype_id column of Data table.

Akshay
  • 1,137
  • 1
  • 10
  • 12
  • Are you sure this code add the new field i tried this code but it replace my contact name with MyFavSong. Why? – kalpana c Oct 31 '13 at 12:05