在 Android 中扩展过滤器时如何使用 publishResults() 方法? [英] How do I use publishResults() method when extending Filters in Android?

查看:19
本文介绍了在 Android 中扩展过滤器时如何使用 publishResults() 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on an autocompletetextview that will work off of a key value system, and am trying to find out what I need to do to make publishResults work, as the results param being passed to publishResults here is correct in the debugger, however I have no idea what it should correspond to or how to cause it to display the results, can anyone help? the creation of this object is in another file, and looks like this:

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

推荐答案

首先不要使用 String 数组.

First of all don't use String array.

要为键值对工作,您可以调整您的 If 语句..在你的 onCreate 中试试这个

to work for key value pair you can adjust your If statement.. try this in your 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 中扩展过滤器时如何使用 publishResults() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆