以编程方式选择AutoCompleteTextView项目 [英] AutoCompleteTextView item selection programmatically

查看:172
本文介绍了以编程方式选择AutoCompleteTextView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自动填充的文本视图,该视图充满了来自sqlite数据库的城市,该数据库在项目单击时调用了异步任务,最近我添加了一个选项来使用gps检测我的位置,所以问题是我可以检测到城市(即贝鲁特)并设置autocompletetextview的文本,但事实是,下拉过滤器打开,显示贝鲁特(这是正确的),但我仍然需要单击列表项以调用侦听器,如何以编程方式做到这一点



操作方法:

-输入活动(完成)

-检测位置(完成)

-设置文本视图的文本(DONE)

-显示文本视图下拉列表(DONE)

-选择将要返回的项目,因为它将仅返回一个城市(未完成)

解决方案

很明显,塔诺(Tano)的解决方案足以回答这个问题。但是,如果其他人遇到了与我相同的用例,这里有一些更多的背景可能会帮助您...



尝试制作一个不可编辑的材料暴露下拉菜单并进行设置它是通过编程方式获得的初始值。您可以在公开的下拉菜单部分的此处,这表明使用 TextInputLayout AutocompleteTextView 的机制(即使您不希望使用自动完成功能)。 / p>

失败的解决方案1:
乍一看 setListSelection() getListSelection()似乎可以解决问题。但是经过多次试验,我了解到它们可能还不够,因为它们仅在列表弹出 isShowing()时才起作用。因此,例如,如果您只想设置初始选择而不必先显示列表弹出窗口,则此方法将无效。



失败的解决方案2:
然后,我尝试了 setText()在文本框中显示正确的文本。好极了!可是等等!当我单击文本视图时,由于某种原因,列表弹出窗口中仅显示选项的子集。怎么会这样这里要记住的关键是,由于这是自动完成文本视图,因此默认情况下,它会根据文本视图中的文本过滤选项。这可能并不明显,特别是如果您仅是为了创建一个简单的不可编辑的下拉选择器而使用此控件。



解决方案:
这将我们带到实际解决方案中(由Tano建议)... setText() filter false 将关闭筛选功能,并且不会更改列表弹出窗口的内容。

  autoCompleteTextView.setText(myText,false); 


I have an autocomplete text view that is filled with cities from an sqlite database that calls an async task on item click, recently I added an option to detect my location using the gps, so the problem is I can detect the city (i.e Beirut) and set the text for the autocompletetextview but the thing is that the dropdown filter opens showing Beirut (which is correct) but I still need to click on the list item to invoke the listener, how to do so programmatically

How to:
- Enter the Activity (DONE)
- Detect location (DONE)
- set text of text view (DONE)
- show textview dropdown list(DONE)
- choose the item that will be returned, since it will only return one city (NOT DONE)

解决方案

To be clear, Tano's solution is sufficient to answer this question. But, in case others run into the same use case I did, here's some more background that may potentially help you...

I had been running into this issue specifically while trying to make a non-editable Material Exposed Dropdown Menu and set it's initial value programmatically. The documentation to create this type of "dropdown" can be found in the Exposed Dropdown Menus section here, which suggests a mechanism using TextInputLayout and AutocompleteTextView (even if you don't want autocomplete functionality).

Failed Solution 1: At first glance setListSelection() and getListSelection() seemed like they might do the trick. But after many trials, I learned that they may not be sufficient because they only work when the list popup isShowing(). So for example, if you simply want to set the initial selection without having to show the list popup first, this will not work.

Failed Solution 2: Then I tried setText() which showed the proper text in my textbox. Yay! But wait! When I clicked on the text view, only a subset of options in the list popup were shown for some reason. Why was that? The key thing to keep in mind here is that since this is an autocomplete textview, it by default filters out options based off of the text in the textview. This might not be apparent, especially if you're solely using this control for the sake of making a simple non-editable dropdown selector.

Solution: This brings us to our actual solution (suggested by Tano)... setText() with filter as false will turn off the filtering capabilities AND it will not change the contents of your list popup.

autoCompleteTextView.setText(myText, false);

这篇关于以编程方式选择AutoCompleteTextView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆