且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式选择AutoCompleteTextView项目

更新时间:2023-12-03 14:54:58

很明显,塔诺(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);