我制作了一款名为Location Texter的Android应用。 除了我想要查找联系人时,它的工作正常我只希望显示带有电话号码的联系人。目前它显示所有联系人。 这是代码:
public void showContacts(Main main, Intent data) {
Cursor cursor = main.managedQuery(data.getData(), null, null, null, null);
cursor.moveToNext();
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Cursor phones = main.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext()) {
String phoneNo = phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
main.numbers.add(phoneNo);
}
main.pickPhoneNos(name);
phones.close();
}
它的作用是打开所有联系人的意图。当选择其中一个时,它将返回主要活动。如果所选择的联系人拥有多个移动电话号码,则会显示一个对话框,要求用户选择其中一个。我注意到的是我的一些尝试中没有出现对话。 如果难以理解我的目的,请下载该应用程序。
答案 0 :(得分:0)
您需要更具体地回答您的问题,很难理解您的要求。 我可以看到代码中的一些问题。
cursor
和phones
光标上致电moveToFirst()。缺少对这些函数的调用可能会进一步调用方法崩溃。
答案 1 :(得分:0)
添加:
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" = "+contactId,null, null);
while (phones.moveToNext())
{
...
}
...
}