自定义AutoCompleteTextView适配器导致错误:适配器的内容已更改但ListView未收到通知

时间:2014-12-04 09:35:30

标签: android multithreading listview autocompletetextview notifydatasetchanged

我的应用中有地方的AutoCompleteTextView。 我为它设置了一个进度条。 我在发送httprequest之前显示进度条,当我得到结果时我隐藏了它。

以下是我的适配器的代码:

public class PlacesAutoCompleteAdaptorForCreate extends ArrayAdapter<Vicinity> implements Filterable {
private ArrayList<Vicinity> resultList;
private Context context;
private boolean canContainPropertyRecordsOnly = false;
ProgressBar pb ;

private ArrayList<Vicinity> autocomplete(String input) {

    JSONObject joff=null;
    try {
        new Gogije2();
        ((Activity)context).findViewById(R.id.progress_vicinity_create_page).post(new Runnable() {
            @Override
            public void run() {
                ProgressBar pb = (ProgressBar) ((Activity)context).findViewById(R.id.progress_vicinity_create_page);
                pb.setVisibility(View.VISIBLE);
            }
        });
        JsonRequestForVicinitySearch jsonRequest = new JsonRequestForVicinitySearch(input,canContainPropertyRecordsOnly);
        JSONParserForVicinity jsonParser = new JSONParserForVicinity("https://www.**.com/**");
        joff = jsonParser.getJSONFromUrl(jsonRequest);
        ((Activity)context).findViewById(R.id.progress_vicinity_create_page).post(new Runnable() {
            @Override
            public void run() {
                ProgressBar pb = (ProgressBar) ((Activity)context).findViewById(R.id.progress_vicinity_create_page);
                pb.setVisibility(View.GONE);
            }
        });
        JSONArray predsJsonArray = joff.getJSONArray("Vicinities");
        resultList = new ArrayList<Vicinity>(predsJsonArray.length());
        for(int i = 0; i < predsJsonArray.length(); i++){
            JSONObject c = predsJsonArray.getJSONObject(i);
            Vicinity vicinity = new Gson().fromJson(c.toString(), new Vicinity().getClass());
            resultList.add(vicinity);
        }
        /** If the user entered characters matches no vicinity, we need to show him a message saying "nothing found" */
        if (resultList.size()==0){
            Vicinity emptyMessage = new Vicinity();
            emptyMessage.setText(Farsi.Convert(MainActivity.mContext.getString(R.string.no_vicinity_match)));
            /** I set the id of the empty message to -1, just in case the user clicks on the empty message the app does not crash.  */
            emptyMessage.setID(-1);
            resultList.add(emptyMessage);
        }
    } 
    catch (JSONException e) {
        e.printStackTrace();
    }
    catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

    return resultList;
}
//****************************

public PlacesAutoCompleteAdaptorForCreate(CreatePropertyActivity1 context, int listItem, int vicinityItem, boolean b) {
    super(context, listItem, vicinityItem);
    this.context = context;
    this.canContainPropertyRecordsOnly = b;
}

@Override
public int getCount() {
    if (resultList == null){
        Log.e("track me", "track me");
        return 0;
    }
    return resultList.size();
}

@Override
public Vicinity getItem(int index) {
    if (resultList.size() > index)
        return resultList.get(index);
    else { 
        Log.e("index of vicinity out of bound", "size:"+resultList.size()+"  index:"+index);
        return null;
    }
}

@Override
public Filter getFilter() {
    Filter filter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            if (constraint != null) {
                resultList = autocomplete(constraint.toString());
                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;
}

}

在我的活动中,我设置了适配器:

    PlacesAutoCompleteAdaptorForCreate adaptor = new PlacesAutoCompleteAdaptorForCreate(this, R.layout.list_item_for_create, R.id.vicinity_item_for_create, true); 
    autoCompView.setAdapter(adaptor);

我的问题是我收到以下错误:

E / AndroidRuntime(8473):java.lang.IllegalStateException:适配器的内容已更改,但ListView未收到通知。确保不从后台线程修改适配器的内容,而只是从UI线程修改。 [在ListView(-1,类android.widget.ListPopupWindow $ DropDownListView)中使用Adapter(类com。 .Utility.PlacesAutoCompleteAdaptorForCreate)]

当我调用notifyDateSetChanged时(在适配器中,更改结果列表后)我得到异常,说我无法触摸UI线程外的列表,这是合理的,但我不知道我还能做什么!

我是初学者,这是我的第一个应用。任何帮助表示赞赏。 提前谢谢。

编辑: 当我删除进度条的部件时,问题就解决了!!

0 个答案:

没有答案