为什么我不能动态更新AutoCompleteTextView?

时间:2013-04-07 09:41:32

标签: android adapter autocompletetextview

我使用ArrayList和ArrayAdapter来记录AutoCompleteTextView的内容,我想在修改列表时动态刷新AutoCompleteTextView。列表的修改在webviewClient中触发。

但是,我的代码不起作用。只有在重新加载整个活动时才能更新AutoCompleteTextView。根本没有动态更新。谁能帮我找出问题所在?

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mHistory); //mHistory is the list of data
    adapter.setNotifyOnChange(true);
    mEditText1.setAdapter(adapter); //mEditText1 is the AutoCompleteTextView

    mWebView1.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url){
            super.onPageFinished(view, url);

            if (!mHistory.contains(mWebView1.getUrl())){
                mHistory.add(mWebView1.getUrl());
                writeList(view.getContext(), mHistory, "history");  //this is to store the list elsewhere
            }
            adapter.notifyDataSetChanged();

        }

    });         

2 个答案:

答案 0 :(得分:5)

我建议您尝试不要修改列表mHistory,而是使用ArrayAdapteradd()等来修改您的clear()

adapter.add(mWebView1.getUrl());

此外,如果这不会有帮助,您可以尝试创建自己的自定义适配器,而不是默认的ArrayAdapter。你可以通过公共功能强制过滤callFiltering自定义适配器可能是这样的:

public class CustomArrayAdapter extends ArrayAdapter<String> implements
        Filterable {
    private List<String> list;
    private CustomFilter customFilter;

    public CustomArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        list = new ArrayList<String>();
    }

    @Override
    public void add(String object) {
        list.add(object);
        notifyDataSetChanged();
    }

    @Override
    public void clear() {
        list.clear();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public String getItem(int position) {
        return list.get(position);
    }

    @Override
    public CustomFilter getFilter() {
        if (customFilter == null) {
            customFilter = new CustomFilter();
        }
        return customFilter;
    }

    public void callFiltering(String term) {
        customFilter.performFiltering(term);
    }

    private class CustomFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint != null) {
                results.values = list;
                results.count = list.size();
            }
            return results;
        }

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

    }

}

答案 1 :(得分:-1)

如果添加以下行:

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mHistory);

adapter.notifyDataSetChanged();之前,它应该有用。

据我所知,ArrayAdapter接受一个List(在你的情况下是mHistory)并调用它像mOriginalValues。然后根据在ACTV中键入的内容过滤它,从mOriginalValues创建自己的列表,然后再显示它。

adapter.notifyDataSetChanged()指的是已过滤的列表,而不是mOriginalValues。因此,您必须创建一个新适配器并将其提供给新列表。