Android联系人--群组分组查询

Android联系人--群组分组查询

/**
	 * 当Groups.DELETED=0的时候, 是查询没有被删除的联系人分组
	 */
	public void getContactsGroups() {
		String[] RAW_PROJECTION = new String[] { ContactsContract.Groups._ID,
				ContactsContract.Groups.TITLE, };
		String RAW_CONTACTS_WHERE = ContactsContract.Groups.DELETED + " = ? ";
		Cursor cursor = getContentResolver().query(
				ContactsContract.Groups.CONTENT_URI, RAW_PROJECTION,
				RAW_CONTACTS_WHERE, new String[] { "" + 0 }, null);
		while (cursor.moveToNext()) {
			String id = cursor.getString(cursor.getColumnIndex("_id"));
			String title = cursor.getString(cursor.getColumnIndex("title"));
			Log.e("XZQ", id + "   " + title);
		}
		cursor.close();
	}

	/**
	 * 当Groups.DELETED=1的时候,是查询删除的分组
	 */
	public void getContactsGroups1() {
		String[] RAW_PROJECTION = new String[] { ContactsContract.Groups._ID,
				ContactsContract.Groups.TITLE };
		String RAW_CONTACTS_WHERE = ContactsContract.Groups.DELETED + " = ? ";
		Cursor cursor = getContentResolver().query(
				ContactsContract.Groups.CONTENT_URI, RAW_PROJECTION,
				RAW_CONTACTS_WHERE, new String[] { "" + 1 }, null);
		while (cursor.moveToNext()) {
			String id = cursor.getString(cursor.getColumnIndex("_id"));
			String title = cursor.getString(cursor.getColumnIndex("title"));
			Log.e("TAG", id + "   " + title);
		}
		cursor.close();
	}

	/**
	 * 默认情况下查询所有的分组
	 */
	public void getContactsGroups2() {
		Cursor cur = getContentResolver().query(Groups.CONTENT_URI, null, null,
				null, null);
		while (cur.moveToNext()) {
			int id = cur
					.getInt(cur.getColumnIndex(ContactsContract.Groups._ID));
			String title = cur.getString(cur
					.getColumnIndex(ContactsContract.Groups.TITLE));
			int count=getCountOfGroup(id);
			Log.e("MainActivity", id + "   " + title + "  " + count);
		}
		cur.close();
	}

	/**
	 * @param 群组ID
	 * @return 查询当前分组中有多少个联系人
	 */
	private int getCountOfGroup(int groupId) {
		String selection = ContactsContract.Data.MIMETYPE + "='"
				+ GroupMembership.CONTENT_ITEM_TYPE + "' AND "
				+ ContactsContract.Data.DATA1 + "=" + groupId;
		String[] colvalue = new String[] { ContactsContract.Data.RAW_CONTACT_ID };
		Cursor cursor = getContentResolver().query(
				ContactsContract.Data.CONTENT_URI, colvalue, selection, null,
				"data1 asc");
		int count = cursor.getCount();
		return count;
	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值