Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutocompleteTextView Error: IllegalStateException: The content of the adapter has changed but ListView did not receive a notification

I have an IllegalStateException Error in this class when cancel or type fast on my AutocompleteTextView. I have read something about that but I can't solve this problem. Anyone can correct my code?

Thanks advance for any helpers!! (Sorry for my bad english)

This is the full error:

java.lang.IllegalStateException: The content of the adapter has changed but ListView did      not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(-1, class android.widget.ListPopupWindow$DropDownListView) with Adapter(class com.turkeys.planandgo.Activity.MapActivity$AutoComplete)]

This is my class:

public class AutoComplete extends ArrayAdapter<String> implements Filterable {
    private static final String LOG_TAG = "carEgiri";

    private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
    private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
    private static final String OUT_JSON = "/json";

    private static final String API_KEY = "AIzaSyCDycjwe51YuMe7Sx8nHv9Z6C-kBGPEQ64";

    private ArrayList<String> resultList;

    private ArrayList<String> autocomplete(String input) {
            ArrayList<String> resultList = null;

            HttpURLConnection conn = null;
            StringBuilder jsonResults = new StringBuilder();
            try {
                StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
                sb.append("?sensor=false&key=" + API_KEY);
                sb.append("&input=" + URLEncoder.encode(input, "utf8"));

                URL url = new URL(sb.toString());
                conn = (HttpURLConnection) url.openConnection();
                InputStreamReader in = new InputStreamReader(conn.getInputStream());
                Log.d("====", "Requesst send");
                // Load the results into a StringBuilder
                int read;
                char[] buff = new char[1024];
                while ((read = in.read(buff)) != -1) {
                    jsonResults.append(buff, 0, read);
                }
            } catch (MalformedURLException e) {
                Log.e(LOG_TAG, "Error processing Places API URL", e);
                return resultList;
            } catch (IOException e) {
                Log.e(LOG_TAG, "Error connecting to Places API", e);
                return resultList;
            } finally {
                if (conn != null) {
                    conn.disconnect();
                }
            }

            try {
                // Create a JSON object hierarchy from the results
                Log.d("JSON","Parsing resultant JSON :)");
                JSONObject jsonObj = new JSONObject(jsonResults.toString());
                JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");

                // Extract the Place descriptions from the results
                resultList = new ArrayList<String>(predsJsonArray.length());
                Log.d("JSON","predsJsonArray has length " + predsJsonArray.length());
                for (int i = 0; i < predsJsonArray.length(); i++) {

                    resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
                    Log.d("JSON",resultList.get(i));
                }
            } catch (JSONException e) {
                Log.e(LOG_TAG, "Cannot process JSON results", e);
            }

            return resultList;
        }

    public AutoComplete(Context context, int textViewResourceId) {
        super((Context) context, textViewResourceId);
    }

    @Override
    public int getCount() {
        if (resultList == null)
            return 0;
        return resultList.size();
    }

    @Override
    public String getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults filterResults = new FilterResults();
                    if (constraint != null) {
                        // Retrieve the autocomplete results.
                        resultList = autocomplete(constraint.toString());

                        // Assign the data to the FilterResults
                        filterResults.values = resultList;
                        filterResults.count = resultList.size();
                    }
                    return filterResults;
                }

                @Override
                protected void publishResults(CharSequence constraint,FilterResults results) {
                    if (results != null && results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }
        };
        return filter;
    }

}
like image 879
dvdciri Avatar asked Sep 11 '14 13:09

dvdciri


3 Answers

Please just remove resultList from performFiltering method. performFiltering method is running in background thread. here is more clear information!

like image 63
nAkhmedov Avatar answered Oct 19 '22 16:10

nAkhmedov


I was facing the same problem, after a lot of debugging and research i resolved my issue by overriding notifyDataSetChanged() and evaluating the size of suggestionList. It may help someone. Code snippet is as follows :

private int size = 0; 

@Override
public void notifyDataSetChanged() {
      size = suggestionList.size();
      super.notifyDataSetChanged();
}

and return the size in getCount() as :

@Override
public int getCount() {

   return size; // Return the size of the suggestions list.
}

and the custom filter in My Case(Application) is as follows :

private class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            suggestions.clear();
            FilterResults filterResults = new FilterResults();
            try {

                    if (originalList != null && constraint != null) { // Check if the Original List and Constraint aren't null.
                        try {
                            for (int i = 0; i < originalList.size(); i++) {
                                // if (originalList.get(i).toLowerCase().contains(constraint)) {
                                if (originalList.get(i).toLowerCase().contains(constraint.toString().toLowerCase())) {
                                    suggestionList.add(originalList.get(i)); // If TRUE add item in Suggestions.
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        notifyDataSetChanged();
                    }

            } catch (Exception e) {
                e.printStackTrace();
            }
           // Create new Filter Results and return this to publishResults;
            filterResults.values = suggestionList;
            filterResults.count = suggestionList.size();

            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
           if (results != null && results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
like image 1
Zafar Imam Avatar answered Oct 19 '22 17:10

Zafar Imam


met the same issue I made some changes for solving it: 1) don't make any changes in the dataset in performFiltering(...) method
2) make changes only in publishResults(...) method

so we make something like that :

//suggestions dataset
ArrayList<Item> suggestions = new ArrayList<>();

in filter

@Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
       if (results != null && results.count > 0) {
            // rewrite array or clear it and fill new items in it
            suggestions = (ArrayList<Item>)results.values
            notifyDataSetChanged();
        }
    }

and in the adapter in getCount() method return suggestions size:

@Override
public int getCount() {
    return suggestions.size();
}
like image 1
Vitaliy Avatar answered Oct 19 '22 16:10

Vitaliy