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

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

问题描述

我有一个 AutoCompleteTextView,其中充满了来自 sqlite 数据库的城市,该数据库在单击项目时调用 AsyncTask,最近我添加了一个选项来使用 gps 检测我的位置,所以问题是我可以检测城市(即贝鲁特)并为 AutoCompleteTextView 设置文本但问题是下拉过滤器打开显示贝鲁特(这是正确的)但我仍然需要单击在列表项上调用侦听器,如何以编程方式进行

I have an AutoCompleteTextView that is filled with cities from an sqlite database that calls an AsyncTask 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

如何:

  • 输入活动(完成)
  • 检测位置(完成)
  • 设置文本视图的文本(完成)
  • 显示文本视图下拉列表(完成)
  • 选择将要返回的项目,因为它只会返回一个城市(未完成)

推荐答案

需要说明的是,Tano 的解决方案足以回答这个问题.但是,如果其他人遇到与我相同的用例,这里有一些可能对您有帮助的更多背景......

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...

我在尝试制作不可编辑的 Material Exposed 时遇到了这个问题下拉菜单并以编程方式设置它的初始值.创建这种类型的下拉"的文档可以在 Exposed Dropdown Menus 部分这里中找到,它建议使用 TextInputLayoutAutocompleteTextView(即使您不想要自动完成功能).

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).

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

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.

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

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.

解决方案:这给我们带来了我们的实际解决方案(由 Tano 建议)...... setText()filter as false 将关闭过滤功能和它不会更改列表弹出窗口的内容.

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天全站免登陆