Search code examples
javaandroidandroid-arrayadapterautocompletetextviewandroid-filterable

Android: AutoCompleteTextView and Filter - Typing Delay - Why?


Alright guys, this is driving me crazy and I cannot find a solution on the internet!
I did a new Eclipse project and pasted this code inside:

https://developers.google.com/places/training/autocomplete-android

It works, but if I enter or delete (a) character(s) there is mostly a delay(short freeze) while typing them and the suggestions-dropdown disappears and appears again mostly.
As for my understanding, the filtering itself is done asynchronously, hence from a background thread, so why the short freezes?

My goal is to have a freeze-free AutoCompleteTextView like in Google Play Store.
So do you guys have any advices/workarounds for achieving this?


Solution

  • I solved it by placing these lines of code (see comments):

                @Override
                protected FilterResults performFiltering(CharSequence constraint)
                {
                    FilterResults filterResults = new FilterResults();
    
                    // ADDED CODE: TO FIX ERROR "The content of the adapter has changed but ListView did not receive a notification":
                    ArrayList <String> resultListTemp = new ArrayList <String> ();
    
                    if (constraint != null)
                    {
                        // ADDED CODE: TO STOP TYPING DELAY & TO FIX ERROR: "The content of adapter has changed but ListView did not receive a notification"
                        if (conn != null)
                        {
                            conn.disconnect();
                        }
    
                        // CHANGED CODE: TO FIX ERROR "The content of the adapter has changed but ListView did not receive a notification":
                        resultListTemp.addAll(autocomplete(constraint.toString()));
    
                        filterResults.values = resultListTemp;
                        filterResults.count = resultListTemp.size();
                    }
                    return filterResults;
                }
    
    
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results)
                {
                    // ADDED CODE: TO FIX ERROR "The content of the adapter has changed but ListView did not receive a notification":
                    resultList = (ArrayList <String>) results.values;
    
                    if (results != null && results.count > 0)
                    {
                        notifyDataSetChanged();
                    }
                    else
                    {
                        notifyDataSetInvalidated();
                    }
                }
    
    
                private ArrayList<String> autocomplete(String input)
                {
                    // CHANGED CODE: TO FIX ERROR "The content of the adapter has changed but ListView did not receive a notification":
                    ArrayList<String> resultList = new ArrayList <String> ();
                    .....
                    .....
                }
    



    Where "conn" is the HttpURLConnection for fetching the Google Places data.