java 使用带有 AutoCompleteTextView 的自定义列表适配器,并仍然保持自动完成的功能相同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6116893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:23:37  来源:igfitidea点击:

Using a custom list adapter with the AutoCompleteTextView and still keeping the functionality of the auto complete working the same

javaandroidautocompleteandroid-arrayadapter

提问by xil3

So say I use the following adapter for the AutoCompleteTextView:

所以说我对 AutoCompleteTextView 使用以下适配器:

public class RosterAdapter extends ArrayAdapter<Player> {
...

}

That's using an object called Player, where as the default AutoCompleteTextView works with a String.

这是使用名为 Player 的对象,其中默认的 AutoCompleteTextView 使用字符串。

The list displays fine when I use the custom one, but the only issue I have is when I start typing something, it doesn't display the right things.

当我使用自定义列表时,列表显示正常,但我遇到的唯一问题是当我开始输入内容时,它没有显示正确的内容。

For example - if I start typing bo, I would expect people with the name Bob Henderson, Garry Bobrinski, etc..

例如-如果我开始打字bo,我希望这个名字的人Bob HendersonGarry Bobrinski等等。

But what comes up is the same list, which doesn't seem to matter what I type - just randomly comes up.

但是出现的是同一个列表,这似乎与我输入的内容无关——只是随机出现。

Can I not use a custom object for this to work? Do I have to use a String for it to match the entries properly? Or is there someway I can tell it to look at a specific string for each of the entries?

我不能使用自定义对象来让它工作吗?我是否必须使用字符串才能正确匹配条目?或者有什么办法可以告诉它查看每个条目的特定字符串?

* Update *

* 更新 *

Here's some more code - this is how I set the custom adapter RosterAdapter. This works, but the autocomplete aspect of it doesn't function properly. It's almost like it gets confused and doesn't know what to look for in the object, to match the typed string.

这是更多代码 - 这就是我设置自定义适配器的方式RosterAdapter。这有效,但它的自动完成方面无法正常工作。这几乎就像它变得困惑并且不知道在对象中寻找什么来匹配输入的字符串。

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_players);
            RosterAdapter adapter = new RosterAdapter(RosterActivity.this, R.layout.roster_row, players);
            textView.setAdapter(adapter);

This is using a generic ArrayAdapter, and this works fine for matching the entries:

这是使用通用 ArrayAdapter,这适用于匹配条目:

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_players);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(RosterActivity.this, R.layout.players_autocomplete, players);
            textView.setAdapter(adapter);

回答by Aleadam

It's hard to say for sure without any code, but I believe you might not be implementing getFilter()for your to let the adapter use the Playerobjects as strings.

没有任何代码很难确定,但我相信您可能没有实现getFilter()让适配器将Player对象用作字符串。

For an example (unrelated requirement, but same filter needed), see: How do I Use AutoCompleteTextView and populate it with data from a web API?

有关示例(不相关的要求,但需要相同的过滤器),请参阅:如何使用 AutoCompleteTextView 并使用来自 Web API 的数据填充它?

There's another example here: http://www.sacoskun.com/2008/08/autocompletetextview-with-simpleadapter.html

这里还有另一个例子:http: //www.sacoskun.com/2008/08/autocompletetextview-with-simpladapter.html