通过listview更新联系人备注

时间:2010-10-20 21:43:40

标签: android listview contacts

我想为联系人添加一个布尔属性。为了存储这个属性,我注意到我可以使用自己的mimetype存储自定义注释。

所以我创建了一个列表视图,显示所有联系人。列表视图包含一个复选框和一个文本字段。当复选框的状态发生变化时,我想更新联系人。

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);
        }
    }

}

上述代码在更新联系人时失败。我不知道为什么。任何帮助表示赞赏。

THX

0 个答案:

没有答案