将 Android 自动完成中的选择转换为字符串
我刚刚使用在线网络服务实现了文本字段的自动完成,基于 Stackoverflow 上的这个答案: ArrayAdapter 从 AutoCompleteTextAdapter 中的 Web 服务更新
使用 ArrayAdapter<用户>;实现了 Filterable
,我已经设法让自动完成功能按预期建议我输入条目。 User
是一个 Java Bean,其中包含建议中提供的信息(年龄、姓名等)。
当我选择建议时,自动完成字段将使用“错误”数据填充 - 使用 toString() 方法,而不是 bean 的“名称”属性。
我的问题是:我可以重写(在适配器中)一个方法,该方法将允许我指定如何转换 bean 以便为 AutoCompleteTextView 返回正确的属性吗? (理想情况下,不应更改 User.toString())
谢谢!
I have just implemented an autocompletion for a textfield using an online webservice, based on this answer on Stackoverflow:
ArrayAdapter is updated late from Webservice in AutoCompleteTextAdapter
Using an ArrayAdapter<User> implements Filterable
, I have managed that the autocompletion suggests me entries as intended.User
is a Java Bean that contains information which is presented in the suggestion (age, name, ...).
When I select a suggestion, the Autocomplete field is filled with the 'wrong' data - using the toString()
method, instead of the 'name' property of the bean.
My Question is: Can I override (in the Adapter) a method which will allow me to specify how to convert the bean so that the correct property is returned for the AutoCompleteTextView?
(Ideally, User.toString() should not be changed)
Thx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无需子类化
AutoCompleteTextView
并重写convertSelectionToString
方法。通过覆盖convertResultToString
方法位于(您已经子类化的)ArrayAdapter
中的自定义Filter
中。我遇到了同样的问题:我的
ArrayAdapter
中的自定义对象的toString()
实现不是我可以控制的。我实现了这样的方法:There is no need subclassing
AutoCompleteTextView
and override theconvertSelectionToString
method. The same thing is achievable by overriding theconvertResultToString
method in your customFilter
in (your already subclassed)ArrayAdapter
.I had the same problem: custom objects in my
ArrayAdapter
whosetoString()
implementation wasn't something I could control. I implemented the method like this:搜索结果取决于数据的
toString()
返回的内容。在您的情况下,您需要在toString()
实现中返回name
字段。The search results depend on what the data's
toString()
returns. In your case you need to returnname
field in thetoString()
implementation.我找到了另一种方法:
AutoCompleteTextView 中的方法
convertSelectionToString(Object selectedItem)
可以被子类覆盖以允许自定义转换。这样,就不需要对toString()
方法进行调整。在我看来,这样做的优点是过滤器不仅可以返回字符串列表,还可以返回自定义对象列表,这些对象可以由
getView(intposition, View ConvertView, ViewGroupparent) 使用
适配器构建“更丰富”的建议。明显的缺点是,它需要为每个不能修改 toString() 方法的 Filterresult 类型子类化 AutoCompleteTextView。
对此有何评论?
I have found another way:
The method
convertSelectionToString(Object selectedItem)
in AutoCompleteTextView can be overridden by subclasses to allow for custom conversions. This way, no adjustment to thetoString()
method is required.This - it seems to me - has the advantage that the Filter can return not just a list of Strings but a list of custom objects, which can be used by
getView(int position, View convertView, ViewGroup parent)
of the adapter to construct "richer" suggestions.The obvious disadvantage is that it requires subclassing AutoCompleteTextView for every Filterresult type whose toString() method shall not be modified.
Any comments on this?
如果您从
SimpleCursorAdapter
继承您自己的适配器,则可以在构造函数中的 Adapter 上设置CursorToStringConverter
。If you subclass your own adapter from
SimpleCursorAdapter
, you can set aCursorToStringConverter
on the Adapter in the constructor.