gpt4 book ai didi

java - 自定义自动完成适配器 Android

转载 作者:行者123 更新时间:2023-11-30 03:35:42 26 4
gpt4 key购买 nike

我希望有一个带有用户联系人姓名的自动完成文本框。我的代码如下。

private void getContactNames()
{
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
_contactAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line);

while (cursor.moveToNext())
{
int nameIdx = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String tmp = cursor.getString(nameIdx);
_contactAdapter.add(tmp);

}
}

设置适配器:

    AutoCompleteTextView contactName = (AutoCompleteTextView) findViewById(R.id.contactName);
contactName.setAdapter(_contactAdapter);

当我执行此操作时,适配器中包含所有联系人姓名(238 个联系人)。但是,当我开始在文本框中输入内容时,自动完成没有显示。

有趣的是,当我测试它时这样做:

String[] ab = new String[] {"aaaaa", "bbbbb"};
_contactAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,ab);

在文本框中输入时会显示“aaaaa”和“bbbbb”。

有什么想法吗?

谢谢,

汤姆

*编辑

只是想我会跟进。似乎确实是大量的联系人阻止了它的出现。有什么办法可以解决这个问题?

最佳答案

while (cursor.moveToNext())
{
int nameIdx = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String tmp = cursor.getString(nameIdx);
//_contactAdapter.add(tmp);

// get all names in a new arraylist and then assign it to
arrayList.add(tmp);
}

然后将其分配给您的适配器作为

 _contactAdapter = new ArrayAdapter<String> this,android.R.layout.simple_dropdown_item_1line, arrayList);

关于java - 自定义自动完成适配器 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16668556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com