我试图通过他的名字获取联系人的电话号码,(名字是一个字符串),有人知道怎么做吗?谢谢!
问问题
175 次
2 回答
2
这应该对您有所帮助;将 People.NAME 替换为您想要的名称。
String[] projection = new String[]{
People.NAME,
People.NUMBER
};
Cursor c = ctx.getContentResolver().query(People.CONTENT_URI, projection,
null, null, People.NAME + " ASC");
c.moveToFirst();
int nameCol = c.getColumnIndex(People.NAME);
int numCol = c.getColumnIndex(People.NUMBER);
int nContacts = c.getCount();
do{
// Do your work here
} while(c.moveToNext());
希望能帮助到你。
于 2013-07-30T21:03:16.840 回答
0
应该为你工作。返回分配给此联系人的所有号码的列表
public static List<String> getContactByName(Context context,
String name) {
String[] mProjection = {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor cursor = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
mProjection,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE '%"
+ name + "%'", null, null);
List<String> listOfPhonesAssignedToThisName = new ArrayList<String>();
while(cursor.moveToNext()){
listOfPhonesAssignedToThisName.add(cursor.getString(cursor.getColumnIndexOrThrow("DISPLAY_NAME")));
}
return listOfPhonesAssignedToThisName;
}
注意:这个函数比较所有看起来像你的显示名称的东西。如果你想找到合适的人,你需要调整部分:
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE '%" + name + "%'", null, null);
至: ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE '" + name + "'", null, null);
于 2013-07-30T21:11:35.333 回答