获取属于特定组的联系人列表
Posted
技术标签:
【中文标题】获取属于特定组的联系人列表【英文标题】:Get list of contacts belonging to a specific group 【发布时间】:2011-07-21 04:13:51 【问题描述】:有人知道如何在 android 中获取属于 1 个特定组的联系人列表吗?
我需要这样的东西:
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);
【问题讨论】:
【参考方案1】:这是我使用的,对我来说很好用
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 找到
【讨论】:
我猜CONTACT_ID
是用来查找联系人的?
我认为这有一个错误。例如,如果您的组的 groupID 是“15”。那么如果你创建了一个name="15"的联系人,它就会在光标中列出来!这是因为您的查询只检查 GROUP_ROW_ID=groupID,这意味着:数据表中的 data1=15。您还应该检查数据表中的行是否与组成员资格有关:ContactsContract.Data.MIMETYPE+"="+"'"+ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
@JoonasS 我有点困惑,你是说在查询中添加 "ContactsContract.Data.MIMETYPE+"="+"'"+ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE" 还是替换现有的查询?
我的意思是这是额外的检查。所以选择就像:ContactsContract.Data.MIMETYPE+"="+ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE + " AND " + ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID+"="+groupID.这样,它将选择说明组成员身份的行。如果不检查,它可能会找到另一行与组成员资格无关的行(数据表中有很多行 mimetype)【参考方案2】:
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;
希望这将帮助您根据组 id 找到联系人。
【讨论】:
想使用 CursorLoader 怎么办? 所以所有数据行都有一个 ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME 列?!?【参考方案3】: 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();
然后从你想要的地方调用这个函数
【讨论】:
以上是关于获取属于特定组的联系人列表的主要内容,如果未能解决你的问题,请参考以下文章