3

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?

DroidFox
  • 217
  • 3
  • 10

2 Answers2

1

You must use the following library file.you have to call addTextChangeListener(Listener) on AutoCompletetextView. Inside afterTextChanged(Editable s) you have to mention the functionalities you need to do.

/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.text;

/**
 * When an object of a type is attached to an Editable, its methods will
 * be called when the text is changed.
 */
public interface TextWatcher extends NoCopySpan {
    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * are about to be replaced by new text with length <code>after</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     */
    public void beforeTextChanged(CharSequence s, int start,
                                  int count, int after);
    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * have just replaced old text that had length <code>before</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     */
    public void onTextChanged(CharSequence s, int start, int before, int count);

    /**
     * This method is called to notify you that, somewhere within
     * <code>s</code>, the text has been changed.
     * It is legitimate to make further changes to <code>s</code> from
     * this callback, but be careful not to get yourself into an infinite
     * loop, because any changes you make will cause this method to be
     * called again recursively.
     * (You are not told where the change took place because other
     * afterTextChanged() methods may already have made other changes
     * and invalidated the offsets.  But if you need to know here,
     * you can use {@link Spannable#setSpan} in {@link #onTextChanged}
     * to mark your place and then look up from here where the span
     * ended up.
     */
    public void afterTextChanged(Editable s);
}
raguM.tech.
  • 183
  • 11
  • If its solves your problem. keep an vote on my answer.its one encourages all to answer you. – raguM.tech. Mar 26 '14 at 12:36
  • Thank you raguM.tech. What I have tried already: I wrapped the Google Places Request in a AsyncTask and called it in afterTextChanged, this works but with every character that I type, the dropdown-list disappears and appears again(how can I avoid that so the dropdown-list stays open?) and later I've read, that AutoCompleteTextView is already connected automatically to a TextWatcher and there is no need to add it and to work with Filter instead. Right now Im kinda lost what method to use. Any thoughts? – DroidFox Mar 26 '14 at 12:54
  • then you have to use EndlessAdapter. http://developer.android.com/reference/android/widget/Filter.html http://developer.android.com/reference/android/widget/CursorAdapter.html – raguM.tech. Mar 26 '14 at 13:26
1

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.

DroidFox
  • 217
  • 3
  • 10
  • Thanx! This plus adding a temporary "resultListTemp" in performFiltering will also take care of the error message: "The content of the adapter has changed but ListView did not receive a notification". I think that the Google Places Autocomplete Training should be updated with these things. – DroidFox Mar 27 '14 at 10:59
  • you mentioned the adapter.NotifydataSetChange(). If not use this function to made the changes in ListView. then only listview not update. – raguM.tech. Mar 27 '14 at 11:29
  • @raguM.tech: That's true. notifyDataSetChanged() already is included in the Googgle Places Training example. – DroidFox Mar 27 '14 at 16:43
  • 1
    What to do if I am using volley instead of HttpURLConnection? – aastha gupta Sep 28 '16 at 13:24