Getting the current offer from `AutoCompleteTextView`

How to get current top sentence in AutoCompleteTextView ? I have a suggestion about elements, and I have a text change listener registered. I also have a list on the same screen. When they print, I want to scroll to the current β€œbest” sentence. But I can’t understand how to access current offers or at least the highest offer. I think I'm looking for something like AutoCompleteTextView.getCurrentSuggestions() :

 autoCompleteTextView.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { String currentText = autoCompleteTextView.getText(); String bestGuess = autoCompleteTextView.getCurrentSuggestions()[0]; // ^^^ mewthod doesn't exist doSomethingWithGuess(bestGuess); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // do nothing } public void afterTextChanged(Editable s) { // do nothing } }); 
+7
android autocomplete
source share
2 answers

I did what you want to do with the following code:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.autocomplete_1); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit); textView.setAdapter(adapter); adapter.registerDataSetObserver(new DataSetObserver() { @Override public void onChanged() { super.onChanged(); Log.d(TAG, "dataset changed"); Object item = adapter.getItem(0); Log.d(TAG, "item.toString "+ item.toString()); } }); } 

item.toString will print the text displayed in the first item.

Please note that this will happen even if you do not yet show the popup (suggestions). In addition, you should check if there are any elements that have passed the filter criteria (aka user input).

To solve the first problem:

  int dropDownAnchor = textView.getDropDownAnchor(); if(dropDownAnchor==0) { Log.d(TAG, "drop down id = 0"); // popup is not displayed return; } //do stuff 

To solve the second problem, use getCount> 0

+15
source share

AutoCompleteTextView does not scroll down to the best choice, but narrows the selection as you type. Here is an example: http://developer.android.com/resources/tutorials/views/hello-autocomplete.html

As I see from AutoCompleteTextView , there is no way to get the current list of offers.

The only way is to write your own version of the ArrayAdapter and pass it to AutoCompleteTextView.setAdapter (..). Here is the source for the ArrayAdapter . You only need to change the method in the inner class ArrayFilter.performFiltering () so that it displays FilterResults:

.. add a field to the inner ArrayFilter class:

 public ArrayList<T> lastResults; //add this line 

.. until the end of the method execute Filtering:

  lastResults = (ArrayList<T>) results; // add this line return results; } 

Using it like this (adapted example from the link):

 AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country); CustomArrayAdapter<String> adapter = new CustomArrayAdapter<String>(this, R.layout.list_item, COUNTRIES); textView.setAdapter(adapter); // read suggestions ArrayList<String> suggestions = adapter.getFilter().lastResult; 
+1
source share

All Articles