AutocompleteTextView错误:IllegalStateException:适配器的内容已更改,但ListView没有收到通知 [英] AutocompleteTextView Error: IllegalStateException: The content of the adapter has changed but ListView did not receive a notification
本文介绍了AutocompleteTextView错误:IllegalStateException:适配器的内容已更改,但ListView没有收到通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的AutocompleteTextView上取消或快速键入时,此类中出现IllegalStateException错误.我已经阅读了一些有关此内容的信息,但无法解决此问题. 任何人都可以更正我的代码吗?
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)
这是完整的错误:
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)]
这是我的课程:
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;
}
}
推荐答案
请仅从performFiltering
方法中删除resultList. performFiltering
方法正在后台线程中运行. 此处是更清晰的信息!
Please just remove resultList from performFiltering
method. performFiltering
method is running in background thread. here is more clear information!
这篇关于AutocompleteTextView错误:IllegalStateException:适配器的内容已更改,但ListView没有收到通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文