AutoCompleteTextView item selection programmatically

懵懂的女人 提交于 2019-12-23 09:55:57

问题


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)

Regards,


回答1:


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



回答2:


I was facing a similar problem and this solved my issue. Important is to call setText(, ) in order not to filter with the given text set the second parameter with false. The text will be getted from the dropdown adapter

Solution snippet:

automCompleteTextView.setText(automCompleteTextView.getAdapter().getItem(position).toString(), false);



回答3:


I figure out after dig into the AutoCompleteTextView code on android source code:

fun AutoCompleteTextView.selectItem(text: String, position: Int = 0) {
  this.setText(text)
  this.showDropDown()
  this.setSelection(position)
  this.listSelection = position
  this.performCompletion()
}



回答4:


The problem is that you are setting a text and the AutoCompleteTextView is only showing words that match with that text. A non elegant way of solving this is to set an high threshold (at least the max length of the names of the cities) to force Android to show you all the values of your list (this threshold is the number of characters that the field must have to search similarities).




回答5:


autoComplete.setListSelection(position);



回答6:


Try with adding below after setText() in AutoCompleteTextview:-

autoCompleteTV.setSelection(position);

Updated:

This will work in Spinner and AutoCompleteTextView which has dropdown feature, but it will not work with EditText.

Here you can check docs for AbsSpinner in this link: https://developer.android.com/reference/android/widget/AbsSpinner.html#setSelection(int)



来源:https://stackoverflow.com/questions/44963164/autocompletetextview-item-selection-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!