如何按groupName
过滤返回的联系人?
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
String groupName = parent.getItemAtPosition(pos).toString();
Log.i(this.toString(), String.format("Show Group: %s", groupName));
Cursor cur = this.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (name != null) {
Log.i(this.toString(), String.format("Show: %s", name));
}
}
cur.close();
}
}
答案 0 :(得分:2)
我可以使用以下查询按组过滤联系人:
Cursor c = this.managedQuery(Data.CONTENT_URI, new String[] {
Contacts.DISPLAY_NAME, Contacts._ID },
GroupMembership.GROUP_ROW_ID + " = ?",
new String[] { groupId }, Phone.TIMES_CONTACTED + " DESC");
显然,您需要知道Group Id才能完成此操作,我是如何使用SimpleCursorAdapter对Spinner进行以下Cursor:
Cursor c = managedQuery(Groups.CONTENT_URI,
new String[] { Groups.SYSTEM_ID, Groups._ID,},
Groups.SYSTEM_ID + " NOT NULL",
null, null);
答案 1 :(得分:2)
这适用于获取所有组ID而不仅仅是系统ID:
public HashMap<String, Long> fillGroupsHashMap(HashMap<String, Long> groups) //
{
Cursor tempCur = activity.managedQuery(Groups.CONTENT_URI,
new String[] { Groups._ID, Groups.TITLE }, null, null, null);
tempCur.moveToFirst();
while (tempCur.moveToNext()) //
{
groups.put(tempCur.getString(1), tempCur.getLong(0));
}
return groups;
}
答案 2 :(得分:1)
不幸的是,只有系统组(“对同步适配器具有特殊含义的组”)具有SYSTEM_ID。所以这种方法不是防弹的。
答案 3 :(得分:0)
我对此进行了解释,但最后我使用了密码,现在我只能通过电话号码过滤,避免重复和未使用的群组:
String[] GROUP_PROJECTION = new String[] {
ContactsContract.Groups._ID, ContactsContract.Groups.TITLE,ContactsContract.Groups.SUMMARY_COUNT ,ContactsContract.Groups.SUMMARY_WITH_PHONES };
Cursor c = context.getApplicationContext().getContentResolver().query(
ContactsContract.Groups.CONTENT_SUMMARY_URI,
GROUP_PROJECTION,
ContactsContract.Groups.SUMMARY_WITH_PHONES +"> 0"
, null, ContactsContract.Groups.TITLE + " ASC");