Autocompletetextview Illegalstateexception: The Content Of The Adapter Has Changed But Listview Did Not Receive A Notification
Good day, I have the following code and i cannot understand why i get the above error message sporadically. sometimes i get it.. sometimes i don't but i would like to get rid of it
Solution 1:
Here is the getFilter method:which is working fine
@Overridepublic Filter getFilter() {
Filterfilter=newFilter() {
@Overrideprotected FilterResults performFiltering(final CharSequence constraint) {
finalFilterResultsfilterResults=newFilterResults();
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
return filterResults;
}
@OverrideprotectedvoidpublishResults(CharSequence constraint, final FilterResults results) {
try {
activity.runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
if (results != null && results.count > 0) {
// resultList=(ArrayList<String>)results.values;
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
};
return filter;
}
publicstatic ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
HttpURLConnectionconn=null;
StringBuilderjsonResults=newStringBuilder();
try {
StringBuildersb=newStringBuilder(Constant.PLACES_API_BASE + Constant.TYPE_AUTOCOMPLETE + Constant.OUT_JSON);
sb.append("?key=AIzaSyAlU5QFZWbgInmPlV-LWFlXiRS7pZ8wD8o");
//sb.append("&components=country:au");//sb.append("&location=" + String.valueOf(LocationHelperService.dblLatitude) + "," + String.valueOf(LocationHelperService.dblLongitude));
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
sb.append("&radius=" + String.valueOf(50));
URLurl=newURL(sb.toString());
System.out.println("URL: " + url);
conn = (HttpURLConnection) url.openConnection();
InputStreamReaderin=newInputStreamReader(conn.getInputStream());
// Load the results into a StringBuilderint read;
char[] buff = newchar[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing", 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 resultsJSONObjectjsonObj=newJSONObject(jsonResults.toString());
JSONArraypredsJsonArray= jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = newArrayList<String>(predsJsonArray.length());
mainTextList=newArrayList<String>(predsJsonArray.length());
secondoryTextList=newArrayList<String>(predsJsonArray.length());
for (inti=0; i < predsJsonArray.length(); i++) {
System.out.println(predsJsonArray.getJSONObject(i).getString("description"));
System.out.println("============================================================");
resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
mainTextList.add(predsJsonArray.getJSONObject(i).getJSONObject("structured_formatting").getString("main_text"));
if(predsJsonArray.getJSONObject(i).getJSONObject("structured_formatting").has("secondary_text"))
{
secondoryTextList.add(predsJsonArray.getJSONObject(i).getJSONObject("structured_formatting").getString("secondary_text"));
}
else{
secondoryTextList.add("");
}
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
}
return resultList;
}
Solution 2:
I resolved the same issue after a lot of research and debugging. You can take help from here here
Post a Comment for "Autocompletetextview Illegalstateexception: The Content Of The Adapter Has Changed But Listview Did Not Receive A Notification"