tags:

views:

46

answers:

1

Im using Android 2.1 Api

Hi all,

I need to add a String MIME type into existing contacts for storing a userdefined data apart from Phone numbers,email etc.Please help me how to add that Custom Field from my application.

I request with an example because I am a week old on Android please help me.

A: 

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
Thanx pentium but i could not under which context need to instatiate this saveformality(). Please send me full coded example by that i could understand clearly. There is lot of doubts I had .Mt id [email protected]
silverstone
Thanx pentium but i could not under which context need to instatiate this saveformality(). Please send me full coded example by that i could understand clearly. There is lot of doubts I had .Please i request you to send any fully coded apllication r any example.My id [email protected]
silverstone
Don't expect to receive any email as you won't. This is an example code how to have a custom mimetype on contacts. This is sample code, please take the time and try to understand. For me it's in clsContacts.
Pentium10