android - 在 Android 中扩展过滤器时如何使用 publishResults() 方法?

标签 android filter autocompletetextview

我正在开发一个自动完成 TextView ,它将在键值系统之外工作,并试图找出我需要做些什么才能使 publishResults 工作,因为这里传递给 publishResults 的结果参数在调试器中是正确的,但是我不知道它应该对应什么或者如何让它显示结果,有人可以帮忙吗?这个对象的创建在另一个文件中,看起来像这样:

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();
            }
        }
    }
}

关于android - 在 Android 中扩展过滤器时如何使用 publishResults() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6067446/

相关文章:

android - 确保安装时使用的Node.js版本与运行时使用的版本匹配

php - 阵列相对于另一个的快速过滤器

c - 在 C 中将字符串从一个数组移动到另一个数组

android - 使用 Intent 的空指针异常

android - 如何在 TextView 文本下划线并更改下划线的颜色

android - MPandroidChart - 放大和缩小以仅适合 Y 轴上的值

java - 将带有 css 的 Jsp 转换为 pdf

android - AutoCompleteTextView 下拉菜单的样式

android - AutocompleteTextView 文字不可见

android - 为什么 AutoCompleteTextView 的名字这么奇怪?