将自定义列表适配器与 AutoCompleteTextView 结合使用,并仍然保持自动完成功能相同
假设我对 AutoCompleteTextView 使用以下适配器:
public class RosterAdapter extends ArrayAdapter<Player> {
...
}
即使用名为 Player 的对象,其中默认 AutoCompleteTextView 使用字符串。
当我使用自定义列表时,该列表显示良好,但我遇到的唯一问题是当我开始输入内容时,它不会显示正确的内容。
例如 - 如果我开始输入 bo
,我会期望人们的名字为 Bob Henderson
、Garry Bobrinski
等。
但是会出现什么情况是同一个列表,我输入的内容似乎并不重要 - 只是随机出现。
我可以不使用自定义对象来实现此功能吗?我是否必须使用字符串才能正确匹配条目?或者我可以通过某种方式告诉它查看每个条目的特定字符串吗?
* 更新 *
这里有更多代码 - 这是我设置自定义适配器 RosterAdapter
的方法。这是可行的,但它的自动完成功能无法正常工作。这几乎就像它感到困惑并且不知道要在对象中查找什么来匹配键入的字符串。
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_players);
RosterAdapter adapter = new RosterAdapter(RosterActivity.this, R.layout.roster_row, players);
textView.setAdapter(adapter);
这是使用通用的 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);
So say I use the following adapter for the AutoCompleteTextView:
public class RosterAdapter extends ArrayAdapter<Player> {
...
}
That's using an object called Player, where as the default AutoCompleteTextView works with a String.
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..
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.
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:
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_players);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(RosterActivity.this, R.layout.players_autocomplete, players);
textView.setAdapter(adapter);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有任何代码很难确定,但我相信您可能没有实现
getFilter()
让适配器使用Player
对象作为字符串。有关示例(不相关的要求,但需要相同的过滤器),请参阅:如何使用 AutoCompleteTextView 并使用来自 Web API 的数据填充它?
这里还有另一个示例:http://www.sacoskun.com/2008/08/autocompletetextview-with-简单适配器.html
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 thePlayer
objects as strings.For an example (unrelated requirement, but same filter needed), see: How do I Use AutoCompleteTextView and populate it with data from a web API?
There's another example here: http://www.sacoskun.com/2008/08/autocompletetextview-with-simpleadapter.html