自定义自动完成适配器Android

时间:2013-05-21 11:09:56

标签: 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”。

有什么想法吗?

谢谢,

汤姆

*的 修改

想到我会跟进。它似乎确实是大量的联系阻止它出现。有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

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);