我正在研究一个自动完成的extview,它将在键值系统上运行,并且正在尝试找出我需要做什么来使publishresults工作,因为这里传递给publishresults的results参数在调试器中是正确的,但是我不知道它应该对应什么或者如何对应。o让它显示结果,有人能帮忙吗?此对象的创建位于另一个文件中,如下所示:

autoCompleteBox.setAdapter(new AutoCmpAdapter(this, android.R.layout.simple_dropdown_item_1line));

其余代码如下:
public class AutoCmpAdapter extends ArrayAdapter<String> implements Filterable {

    protected Filter filter;
    protected ArrayList<String> items;
    protected ArrayList<String> res;
    String lWds[] = { "HOMER", "TOM" };
    String sWds[] = { "SIMPSON", "JONES" };

    public AutoCmpAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        filter = new PhysFilter();
        res = new ArrayList<String>();
    }

    public Filter getFilter() {
        return filter;
    }

    private class PhysFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults f = new FilterResults();
            res.clear();
            if (constraint != null) {
                ArrayList<String> res = new ArrayList<String>();
                for (int x = 0; x < sWds.length; x++) {
                    if (sWds[x].toUpperCase().startsWith(constraint.toString().toUpperCase())) {
                        res.add(lWds[x]);
                    }
                }
                f.values = res.toArray();
                f.count = res.size();
            }
            return f;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.count > 0) {
                Log.println(Log.INFO, "Results", "FOUND");
                notifyDataSetChanged();
            } else {
                Log.println(Log.INFO, "Results", "-");
                notifyDataSetInvalidated();
            }
        }
    }
}

最佳答案

首先不要使用字符串数组。
要为键值对工作,可以调整if语句。
在你的oncreate中试试这个

AutoCompleteTextView mAutoCompleteTextView;
ArrayList<String> lWds = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.testAutoComplete);


    final AutoCmpAdapter adapter= new AutoCmpAdapter(this, android.R.layout.simple_dropdown_item_1line,lWds);
    mAutoCompleteTextView.setAdapter(adapter);
    mAutoCompleteTextView.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            adapter.getFilter().filter(s);

        }
    });
}

和适配器类
  public class AutoCmpAdapter extends ArrayAdapter<String> implements Filterable {

    protected Filter filter;
    protected ArrayList<String> items;
    protected ArrayList<String> res;

    String sWds[] = { "SIMPSON", "JONES" };

    public AutoCmpAdapter(Context context, int textViewResourceId,ArrayList<String> listData) {
        super(context, textViewResourceId,0,listData);

        filter = new PhysFilter();
        res = new ArrayList<String>();
    }

    public Filter getFilter() {
        return filter;
    }

    private class PhysFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults f = new FilterResults();
            res.clear();
            if (constraint != null) {
                ArrayList<String> res = new ArrayList<String>();
                for (int x = 0; x < sWds.length; x++) {
                    if (sWds[x].toUpperCase().contains(constraint.toString().toUpperCase())) {
                        res.add(sWds[x]);
                    }
                }
                f.values = res;//.toArray();
                f.count = res.size();
            }
            return f;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.count > 0) {
                Log.println(Log.INFO, "Results", "FOUND");
                lWds.clear();
                lWds.addAll((ArrayList<String>) results.values);
                notifyDataSetChanged();
            } else {
                Log.println(Log.INFO, "Results", "-");
                notifyDataSetInvalidated();
            }
        }
    }
}

08-16 13:13