AutocompleteTextView with asynchronous sentences does not show dropdown menu

I am adding a TextChangedListener to an AutocompleteTextView . In TextChangedListener afterTextChanged() I call AsyncTask , which downloads data from the Internet (downloading all data when the activity starts is not an option, since lists can be quite large, so it becomes just a waste of traffic). AsyncTask onPostExecute() looks like this (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 override toString() . I want my program to start loading data when the user starts to print and show the results immediately after receiving and analyzing them.

My problem:

autocompleteTextView.showDropDown() no effect. onPostExecute() receives the correct list of data, they are added to the adapter, but showDropDown() does not display a drop-down list. What's the matter?

+8
android autocompletetextview
source share
4 answers

I am doing the same thing and I just achieved this functionality. Instead of cleaning the adapter and building it separately, install the adapter as shown below (I do this in the function that is called in 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 the data has been changed.

You can also call

 adapter.setNotifyOnChange(true) 

making the call unnecessary

 adapter.notifyDatasetChanged() 

See setNotifyOnChange . I hope I can handle this.

+19
source share

I also ran into this problem. My completion threshold was 2. When 2 characters were typed, I retrieved data from the server and populated the array adapter, but autoRailwayFrom.showDropDown(); didn't show drop down ...

I just write in a line and it works ...

 autoRailwayFrom.setText(autoRailwayFrom.getText()); adapterFrom.notifyDataSetChanged(); autoRailwayFrom.showDropDown(); 
+6
source share

This question was answered, but I believe that it is not in order - the key for everyone is the filtered interface. Here you have a working adapter example based on the BaseAdapter. The missing part here is the message (I used Retrofit):

 public class UsersAutoAdapter extends BaseAdapter implements Filterable { { DaggerGraphManager.INSTANCE.inject(this); } @Inject CommunicationManager mCommunicationManager; private FindUserResponse mFindUserResponse; @Override public int getCount() { if (mFindUserResponse != null && mFindUserResponse.mUsers != null) { return mFindUserResponse.mUsers.size(); } return 0; } @Override public Object getItem(int i) { return mFindUserResponse.mUsers.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View pView, ViewGroup pViewGroup) { if (pView == null) { LayoutInflater inflater = LayoutInflater.from(pViewGroup.getContext()); pView = inflater.inflate(R.layout.ac_item_user, pViewGroup, false); } TextView textViewUserName = (TextView) pView.findViewById(R.id.text_view_user_name); textViewUserName.setText(mFindUserResponse.mUsers.get(i).mUserName); return pView; } @Override public Filter getFilter() { return new Filter() { //this method is called async (not from UI thread!) so making network request is possible here @Override protected FilterResults performFiltering(CharSequence pCharSequence) { if (pCharSequence != null && pCharSequence.length() >= 3) { FindUserResponse users = mCommunicationManager.findUsersSync(pCharSequence.toString()); FilterResults results = new FilterResults(); results.values = users; results.count = users.mUsers.size(); return results; } return null; } //this is UI thread called method (just after performFiltering returns it results @Override protected void publishResults(CharSequence pCharSequence, FilterResults pFilterResults) { if (pFilterResults != null) { mFindUserResponse = (FindUserResponse) pFilterResults.values; notifyDataSetInvalidated(); } } }; } } 
+1
source share

Faced this problem with Retroft api call. Solved by saving the code below in the Retrofit onResponse method. No need to initialize the adapter inside the onCreate method. My code was similar to below:

 productListAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, productList); autoCompleteTextViewProductSearch.setAdapter(productListAdapter); productListAdapter.setNotifyOnChange(true); productListAdapter.notifyDataSetChanged(); autoCompleteTextViewProductSearch.showDropDown(); 
0
source share

All Articles