过滤AutoCompleteTextView显示部分匹配 [英] Filtering AutoCompleteTextView to show partial match

查看:219
本文介绍了过滤AutoCompleteTextView显示部分匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个AutoCompleteTextView,我想它的工作就像如XXX%%
确实在SQL中。我试图使用过滤的我把它和code运行做到这一点,但它只是显示现在的一切,即使没有部分匹配。任何帮助将是AP preciated。

 公共类codesArrayAdapter扩展ArrayAdapter实现过滤的{    清单<串GT;所有codeS;
    清单<串GT;原来的codeS;    StringFilter滤波器;
    公共codesArrayAdapter(上下文的背景下,INT资源列表与LT;弦乐>键){
        超级(上下文,资源,密钥);        所有codeS =键;
        原来的codeS =键;
    }    公众诠释的getCount(){
        返回所有codes.size();
    }    公共对象的getItem(INT位置){
        返回所有codes.get(位置);
    }    众长getItemId(INT位置){
        返回的位置;
    }    私有类StringFilter扩展过滤器{
        @覆盖
        保护FilterResults performFiltering(CharSequence的约束){            字符串filterString = constraint.toString()与toLowerCase()。            FilterResults结果=新FilterResults();            最终名单<串GT;列表=原来的codeS;            诠释计数=则为list.size();
            最终的ArrayList<串GT; NLIST =新的ArrayList<串GT;(计数);            串filterableString;            的for(int i = 0; I<计数;我++){
                filterableString = list.get(ⅰ);
                如果(filterableString.toLowerCase()。包括(filterString)){
                    nlist.add(filterableString);
                }
            }            results.values​​ = NLIST;
            results.count = nlist.size();            返回结果;
        }        @燮pressWarnings(未登记)
        @覆盖
        保护无效publishResults(CharSequence的约束,FilterResults结果){
            所有codeS =(ArrayList的<串GT;)results.values​​;
            notifyDataSetChanged();
        }    }
}


解决方案

如果这是你的适配器的完整code,你只是错过贯彻用getFilter()方法,即

  @覆盖
公共过滤用getFilter()
{
    返回新StringFilter();
}

Right now I have an AutoCompleteTextView and I want it to work just like the like "%xxx%" does in SQL. I attempted to do it using Filterable I have it and the code runs but it just displays everything now even if there is no partial match. Any help would be appreciated.

public class CodesArrayAdapter extends ArrayAdapter implements Filterable{

    List<String> allCodes;
    List<String> originalCodes;

    StringFilter filter;


    public CodesArrayAdapter(Context context, int resource, List<String> keys) {
        super(context, resource, keys);

        allCodes=keys;
        originalCodes=keys;


    }

    public int getCount() {
        return allCodes.size();
    }

    public Object getItem(int position) {
        return allCodes.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    private class StringFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final List<String> list = originalCodes;

            int count = list.size();
            final ArrayList<String> nlist = new ArrayList<String>(count);

            String filterableString ;

            for (int i = 0; i < count; i++) {
                filterableString = list.get(i);
                if (filterableString.toLowerCase().contains(filterString)) {
                    nlist.add(filterableString);
                }
            }

            results.values = nlist;
            results.count = nlist.size();

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            allCodes = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }

    }
}

解决方案

If this is the complete code of your adapter, you just missed implementing the getFilter() method, i.e.

@Override
public Filter getFilter()
{
    return new StringFilter();
}

这篇关于过滤AutoCompleteTextView显示部分匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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