如何使用publishResults()方法时,延长过滤器在Android的?过滤器、如何使用、方法、Android

2023-09-06 02:14:45 作者:痴心病友

我的工作,将工作掀起了核心价值体系的autocompletetextview,并正尝试找出我需要怎么做才能让publishResults工作,作为结果参数传递给publishResults这里是在调试器中正确,但是我不知道它应该符合或如何使其显示效果,任何人都可以帮忙吗?创建这个对象是在另一个文件中,如下所示:

  autoCompleteBox.setAdapter(新AutoCmpAdapter(这一点,android.R.layout.simple_dropdown_item_1line));
 

和的code中的其余部分如下:

 公共类AutoCmpAdapter扩展ArrayAdapter<字符串>实现过滤的{

    保护过滤器过滤;
    受保护的ArrayList<字符串>项目;
    受保护的ArrayList<字符串>水库;
    字符串lWds [] = {荷马史诗,TOM};
    SWDS的String [] = {辛普森,琼斯};

    公共AutoCmpAdapter(上下文的背景下,INT textViewResourceId){
        超(背景下,textViewResourceId);
        过滤器=新PhysFilter();
        RES =新的ArrayList<字符串>();
    }

    公共过滤用getFilter(){
        返回过滤器;
    }

    私有类PhysFilter扩展过滤器{

        @覆盖
        保护FilterResults performFiltering(CharSequence的约束){
            FilterResults F =新FilterResults();
            res.clear();
            如果(约束!= NULL){
                ArrayList的<字符串> RES =新的ArrayList<字符串>();
                为(中间体X = 0 X  - 其中; sWds.length; X ++){
                    如果(SWDS [X] .toUpperCase()。startsWith(constraint.toString()。与toUpperCase())){
                        res.add(lWds [X]);
                    }
                }
                f.values​​ = res.toArray();
                f.count = res.size();
            }
            返回F;
        }

        @覆盖
        保护无效publishResults(CharSequence的约束,FilterResults结果){
            如果(results.count大于0){
                Log.println(Log.INFO,结果,发现);
                notifyDataSetChanged();
            } 其他 {
                Log.println(Log.INFO,结果, - );
                notifyDataSetInvalidated();
            }
        }
    }
}
 

解决方案

首先不使用字符串数组。

佳木斯不锈钢真空脱气机用途

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

  AutoCompleteTextView mAutoCompleteTextView;
ArrayList的<字符串> lWds =新的ArrayList<字符串>();
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    mAutoCompleteTextView =(AutoCompleteTextView)findViewById(R.id.testAutoComplete);


    最后AutoCmpAdapter适配器=新AutoCmpAdapter(这一点,android.R.layout.simple_dropdown_item_1line,lWds);
    mAutoCompleteTextView.setAdapter(适配器);
    mAutoCompleteTextView.addTextChangedListener(新TextWatcher(){

        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                之后INT){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效afterTextChanged(编辑S){
            。adapter.getFilter()过滤器(S);

        }
    });
}
 

和适配器类如

 公共类AutoCmpAdapter扩展ArrayAdapter<字符串>实现过滤的{

    保护过滤器过滤;
    受保护的ArrayList<字符串>项目;
    受保护的ArrayList<字符串>水库;

    SWDS的String [] = {辛普森,琼斯};

    公共AutoCmpAdapter(上下文的背景下,INT textViewResourceId,ArrayList的<字符串>的ListData){
        超级(上下文,textViewResourceId,0,的ListData);

        过滤器=新PhysFilter();
        RES =新的ArrayList<字符串>();
    }

    公共过滤用getFilter(){
        返回过滤器;
    }

    私有类PhysFilter扩展过滤器{

        @覆盖
        保护FilterResults performFiltering(CharSequence的约束){
            FilterResults F =新FilterResults();
            res.clear();
            如果(约束!= NULL){
                ArrayList的<字符串> RES =新的ArrayList<字符串>();
                为(中间体X = 0 X  - 其中; sWds.length; X ++){
                    如果(SWDS [X] .toUpperCase()。包含(constraint.toString()。与toUpperCase())){
                        res.add(SWDS [X]);
                    }
                }
                f.values​​ =水库; //的toArray();
                f.count = res.size();
            }
            返回F;
        }

        @燮pressWarnings(未登记)
        @覆盖
        保护无效publishResults(CharSequence的约束,FilterResults结果){
            如果(results.count大于0){
                Log.println(Log.INFO,结果,发现);
                lWds.clear();
                lWds.addAll((ArrayList中<字符串>)results.values​​);
                notifyDataSetChanged();
            } 其他 {
                Log.println(Log.INFO,结果, - );
                notifyDataSetInvalidated();
            }
        }
    }
}
 

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

and the rest of the code is as follows:

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

解决方案

First of all don't use String array.

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

        }
    });
}

and adapter class like

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