【问题标题】:Custom Autocomplete Adapter Android自定义自动完成适配器 Android
【发布时间】:2013-05-21 11:09:55
【问题描述】:

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

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”。

有什么想法吗?

谢谢,

汤姆

*编辑

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

【问题讨论】:

标签: java android


【解决方案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);

【讨论】:

  • 您好,感谢您的回复,可惜没有用。字符串在适配器的“mObjects”成员中,但在我开始输入框中时不会出现。是不是因为弦太长了?或者我的元素太多(238)。干杯
  • 尝试设置阈值然后contactName.setThreshold(1);并且 String 是类类型,所以不用担心你的字符串很容易适应它的限制
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
相关资源
最近更新 更多