Android 如何在AutoCompleteTextView中显示联系人列表名称
我正在尝试使用带有AutoCompleteTextView的电话联系人列表中的姓名,因此当用户开始键入姓名时,它将显示从其联系人自动完成的建议。 另外,它是在自定义对话框中完成的 但它不起作用 以下是相关代码:Android 如何在AutoCompleteTextView中显示联系人列表名称,android,Android,我正在尝试使用带有AutoCompleteTextView的电话联系人列表中的姓名,因此当用户开始键入姓名时,它将显示从其联系人自动完成的建议。 另外,它是在自定义对话框中完成的 但它不起作用 以下是相关代码: AutoCompleteTextView friendName; case R.id.add_debt_menu: // custom dialog final Dialog dialog = new Dialog(t
AutoCompleteTextView friendName;
case R.id.add_debt_menu:
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_debt_dialog);
dialog.setTitle("Add Debt");
friendName = (AutoCompleteTextView)dialog.findViewById(R.id.name);
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
while (cursor.moveToNext()){
name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
ArrayList<String> contacts = new ArrayList<>();
contacts.add(name);
}
cursor.close();
ArrayAdapter<String> adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,contacts);
friendName.setAdapter(adapter);
所以关键是使用CommonDataTypes。我仍然不知道为什么它不能与其他方法一起使用。请参见@Varundroid谢谢,该示例提示我使用CommonDataTypes而不是Contacts contract.Contacts.DISPLAY\u NAME,它成功了,我用contacts contract.commondatatypes.Phone.DISPLAY代替了电子邮件_NAME@BaradDedi你为什么要使用ArrayAdapter???@pskink用于AutoCompleteTextView这是我在所有示例中看到的,除了你链接的内容,我想,没有看到你的方法在任何地方使用,你说它更好?我甚至没有尝试过它,因为我甚至看不到它如何连接到AutoCompleteTextView,因为没有findViewById
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));