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

标签 java android

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

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/

相关文章:

Java 继承问题 - 尝试理解这个概念

java - jdbc :hsqldb:hsqlTempFiles mean 是什么意思

android - Repo sync后目录下没有文件

c# - 无法将 Google Play 服务添加到 Xamarin Android 项目

java - 如何在 Eclipse 中正确导入这个库?

java - 有多少人可以连接到ServerSocket?

java - 无法让瓷砖与 spring mvc 3 一起使用

java - onCreate() 方法中的 FileNotFoundException

java - Android图片设置高度问题

android - 命名功能模块包