android – Update contact notes through listview – Education Career Blog

I’d like to add a boolean property to a contact. To store this property I noticed that I can store custom notes with my own mimetype.

So I created a listview which displays all contacts. The listview contains a checkbox and a text field. When the status of the checkbox changes I’d like to update the contact.

public static final String MY_MIME_TYPE = "vnd.android.cursor.item/vnd.myapp.boolean_property";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
            ContactsContract.Contacts.DISPLAY_NAME);

    setListAdapter(new MyAdapter(this, c));
    getListView().setItemsCanFocus(false);
}

public class MyAdapter extends SimpleCursorAdapter implements OnCheckedChangeListener {

    public MyAdapter(Context context, Cursor contactCursor) {
        super(context, R.layout.my_item, contactCursor, new String { Contacts._ID, Contacts.DISPLAY_NAME },
                new int { R.id.myName });
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
        String noteWhereParams = new String { cursor.getString(cursor.getColumnIndex(Contacts._ID)),
                MY_MIME_TYPE };
        Cursor noteCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, noteWhere,
                noteWhereParams, null);
        CheckBox isTrue = (CheckBox) view.findViewById(R.id.isTrue);
        TextView myName = (TextView) view.findViewById(R.id.myName);
        isTrue.setOnCheckedChangeListener(null);
        if (noteCur.moveToFirst()) {
            int note = noteCur.getInt(noteCur.getColumnIndex(ContactsContract.Data.DATA1));
            isTrue.setChecked(note != 0);
        } else {
            isTrue.setChecked(false);
        }
        isTrue.setOnCheckedChangeListener(this);
        noteCur.close();

        isTrue.setTag(cursor.getString(cursor.getColumnIndex(Contacts._ID)));
        myName.setText(cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME)));
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        CheckBox c = (CheckBox) buttonView;
        String id = (String) c.getTag();
        try {
            ContentValues values = new ContentValues();
            values.put(ContactsContract.Data.DATA1, isChecked ? "1" : "0");
            values.put(ContactsContract.Data.RAW_CONTACT_ID, id);

            Cursor rawContactsCursor = getContentResolver().query(RawContacts.CONTENT_URI,
                    new String { RawContacts._ID }, RawContacts.CONTACT_ID + "=?",
                    new String { String.valueOf(id) }, null);

            if (rawContactsCursor.moveToFirst()) {
                do {
                    int rawID = rawContactsCursor.getInt(0);
                    int mod = Pilots.this.getContentResolver().update(
                            ContactsContract.Data.CONTENT_URI,
                            values,
                            ContactsContract.Data.RAW_CONTACT_ID + "=" + rawID + " AND "
                                    + ContactsContract.Data.MIMETYPE + "= '" + MY_MIME_TYPE + "'", null);
                    if (mod == 0) {
                        values.put(ContactsContract.Data.MIMETYPE, MY_MIME_TYPE);
                        MyActivity.this.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
                    }
                } while (rawContactsCursor.moveToNext());
            }
            rawContactsCursor.close();
        } catch (Exception e) {
            Log.v(this.getClass().getName(), "save failed", e);
        }
    }

}

The code above fails when it comes to updating the contact. I have no clue why. Any help is appreciated.

Thx

Leave a Comment