Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutocompleteTextView with async suggestions doesn't show dropdown

I add TextChangedListener to AutocompleteTextView. In TextChangedListener's afterTextChanged() I invoke AsyncTask which loads data from web (loading all the data when activity starts is not an option because lists can be pretty large, so it becomes just waste of traffic). AsyncTask's onPostExecute() looks like that (I use ArrayAdapter):

    @Override
    protected void onPostExecute(ArrayList<Subregion> result)
    {
        super.onPostExecute(result);
        if (result != null)
        {
            adapter.clear();
            for (Iterator<Subregion> iterator = result.iterator(); iterator.hasNext();)
            {
                Subregion subregion = iterator.next();
                adapter.add(subregion);
            }
            adapter.notifyDataSetChanged();
            autocompleteTextView.showDropDown();
        }
    }

Subregion is my custom object with overriden toString(). I want my program to start loading data when user starts typing and show results at once they are received and parsed.

My problem:

autocompleteTextView.showDropDown() has no effect. onPostExecute() receives correct list of data, they are added to adapter, but showDropDown() doesn't show the dropdown. What's the matter?

like image 292
Eugene Chumak Avatar asked May 28 '12 15:05

Eugene Chumak


1 Answers

I am doing the exact same thing, and I just achieved this functionality. Instead of clearing the adapter and building it individually, set the adapter as below(I do this in a function which is called in the onPostExecute);

//suggestions is a string array of suggestions.
suggestAdapter = new ArrayAdapter<String>(this, R.layout.suggestions, suggestions);
//The autocomplete view 
suggestions.setAdapter(this.suggestAdapter);
suggestAdapter.notifyDataSetChanged();

You do not need to explicitly call showdropdown, the autocomplete view is automatically updated when the adapter notifies it that data has been changed.

You also can call

adapter.setNotifyOnChange(true)

which makes it unnecessary to call

adapter.notifyDatasetChanged()

See setNotifyOnChange Hope I could be of help.

like image 86
Awemo Avatar answered Sep 17 '22 15:09

Awemo