关于联系人的修改[目前只是这几个想法],基于2.1

看了一些Android开源小应用的写法,感觉好像代码都比较乱,各种各样的都有
虽然自己写的代码也很糟糕,但还是能感觉到在Android开发方面的代码比JEE的要乱很多
着重点不一样吧

联系人修改
1、根据已有的数据,特别是id,查询出要修改哪几条数据,然后一条条去修改
这是一种方法,但我目前没有采取这种方法,因为服务器端返回的数据中只包含了一些数据,没有很多像主键这样的东西,所以我采取了另外一种写法

2、删除data表中有关的数据,然后再新增,这是我目前采用的方法,raw_contacts表和contacts表数据不删除,只是修改
基本代码类似下面

				// 已知一个ID了,要修改他下面的数据
// 删除他下面的DATA数据,然后再新增数据
long id = 3l;
// 根据id查询出rawid
// 可能有多个rawid
Cursor cursor = rawContactsCursor(getApplicationContext(), id);
while (cursor.moveToNext()) {
long rawid = cursor.getLong(cursor
.getColumnIndex(RawContacts._ID));
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

ops.add(ContentProviderOperation
.newDelete(Data.CONTENT_URI).withSelection(
Data.RAW_CONTACT_ID + "=?",
new String[] { String.valueOf(rawid) })
.build());
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI).withValue(
Data.RAW_CONTACT_ID, rawid).withValues(
getNameNewCV()).build());

ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI).withValue(
Data.RAW_CONTACT_ID, rawid).withValues(
getPhoneNewCV()).build());
try {
ContentProviderResult[] rst = getContentResolver()
.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
}

	private Cursor rawContactsCursor(Context ctx, long id) {
return ctx.getContentResolver().query(
ContactsContract.RawContacts.CONTENT_URI, null,
ContactsContract.RawContacts.CONTACT_ID + "=?",
new String[] { String.valueOf(id) }, null);
}

	public ContentValues getNameNewCV() {
ContentValues cv = new ContentValues();
cv.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
cv.put(StructuredName.DISPLAY_NAME, "武 体");
cv.put(StructuredName.GIVEN_NAME, "体");
cv.put(StructuredName.FAMILY_NAME, "武");
return cv;
}

	public ContentValues getPhoneNewCV() {
ContentValues cv = new ContentValues();
cv.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
cv.put(Phone.NUMBER, "666666");
cv.put(Phone.TYPE, Phone.TYPE_COMPANY_MAIN);
return cv;
}


备注:这样修改还是有问题的,貌似contacts会生成新的记录,这样对于需要contact_id的情况就是悲剧啊

下面删除联系人的代码也还是有问题的
				// 删除联系人
// 删除Data
long id = 1l;
// 根据id查询出rawid
// 可能有多个rawid
Cursor cursor = rawContactsCursor(getApplicationContext(), id);

ArrayList<ContentProviderOperation> ops = null;
while (cursor.moveToNext()) {
long rawid = cursor.getLong(cursor
.getColumnIndex(RawContacts._ID));
ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation
.newDelete(Data.CONTENT_URI).withSelection(
Data.RAW_CONTACT_ID + "=?",
new String[] { String.valueOf(rawid) })
.build());
ops.add(ContentProviderOperation.newDelete(
ContentUris.withAppendedId(RawContacts.CONTENT_URI,
rawid)).build());

ops.add(ContentProviderOperation.newDelete(
ContentUris
.withAppendedId(Contacts.CONTENT_URI, id))
.build());

try {
ContentProviderResult[] rst = getContentResolver()
.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值