获取属于特定组的联系人列表

有谁知道如何获得属于Android中的一个特定组的联系人列表?

我需要这样的东西:

Select * from contacts where group_id = "1234" 

我能够通过使用像这样的东西获得所有联系人或所有组的列表:

 Cursor groupCursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); final ArrayList<String> contacts = new ArrayList<String>(); while(groupCursor.moveToNext()) { String name = groupCursor.getString(groupCursor.getColumnIndex(ContactsContract.Constacts.DisplayName )); contacts.add(name); } 

Related of "获取属于特定组的联系人列表"

这是我使用的,它适用于我

 Uri groupURI = ContactsContract.Data.CONTENT_URI; String[] projection = new String[]{ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID , ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID}; Cursor c = managedQuery(groupURI, projection, ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID+"="+groupID, null,null); 

这需要你已经有了组ID,可以通过查询ContactsContract.Groups来find

 public void getSampleContactList(int groupID) { contactList = new ArrayList<ConatctData>(); Uri groupURI = ContactsContract.Data.CONTENT_URI; String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID }; Cursor c = getContentResolver().query( groupURI, projection, ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "=" + groupID, null, null); while (c.moveToNext()) { String id = c .getString(c .getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID)); Cursor pCur = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); while (pCur.moveToNext()) { ConatctData data = new ConatctData(); data.name = pCur .getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); data.phone = pCur .getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); contactList.add(data); } pCur.close(); } } class ConatctData { String phone, name; } 

希望这会帮助你find联系人根据组ID。

  void getlistofcontacts( long groupId ) { String[] cProjection = { Contacts.DISPLAY_NAME, GroupMembership.CONTACT_ID }; Cursor groupCursor = getContentResolver().query( Data.CONTENT_URI, cProjection, CommonDataKinds.GroupMembership.GROUP_ROW_ID + "= ?" + " AND " + ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "='" + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + "'", new String[] { String.valueOf(groupId) }, null); if (groupCursor != null && groupCursor.moveToFirst()) { //Toast.makeText(this,"if",1000).show(); do { int nameCoumnIndex = groupCursor.getColumnIndex(Phone.DISPLAY_NAME); String name = groupCursor.getString(nameCoumnIndex); long contactId = groupCursor.getLong(groupCursor.getColumnIndex(GroupMembership.CONTACT_ID)); Cursor numberCursor = getContentResolver().query(Phone.CONTENT_URI, new String[] { Phone.NUMBER }, Phone.CONTACT_ID + "=" + contactId, null, null); if (numberCursor.moveToFirst()) { int numberColumnIndex = numberCursor.getColumnIndex(Phone.NUMBER); do { String phoneNumber = numberCursor.getString(numberColumnIndex); Toast.makeText(this,name+phoneNumber,1000).show(); } while (numberCursor.moveToNext()); numberCursor.close(); } else { Toast.makeText(this,"no contact are there",1000).show(); } } while (groupCursor.moveToNext()); groupCursor.close(); } else { Toast.makeText(this,"no such group exists",1000).show(); } } 

并从你想要的地方调用这个函数