`
lucane
  • 浏览: 119068 次
  • 性别: Icon_minigender_1
  • 来自: 江湖
社区版块
存档分类
最新评论

关于联系人的修改[目前只是这几个想法],基于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();
					}
				}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics