【问题标题】:Reading a specific contact from its ID on android database从 android 数据库上的 ID 读取特定联系人
【发布时间】:2012-08-03 06:34:06
【问题描述】:
我需要将光标设置到我有其 id 的特定联系人
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null,null, null, null);
我有所有数据库的代码,我该如何优化它?
我需要光标返回联系人的姓名和默认号码
谢谢
【问题讨论】:
标签:
android
sqlite
android-contacts
【解决方案1】:
您实际上需要两个游标。一个用于姓名和身份证,另一个用于电话号码。
Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (c.moveToFirst()) {
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
fullName = c.getString(c.getColumnIndex("display_name_alt"));
id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(c.getString(c.getColumnIndex
(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//phone number
Cursor phones = getContentResolver().query(Phone.CONTENT_URI,
null,
Phone.CONTACT_ID + " = " + id,
null,
null);
while (phones.moveToNext()) {
String number_type = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.TYPE));
if(number_type.equalsIgnoreCase("1"))
number1 = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(number_type.equalsIgnoreCase("2"))
number2 = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}
c.close();
【解决方案2】:
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
Phone.DISPLAY_NAME + " ASC");