通过getFilter方法搜索列表中的字符串数组

时间:2017-06-06 11:21:22

标签: android search arraylist filter adapter

我尝试在自定义适配器中使用getFilter方法实现searchview。

我的Model类中有一个getGenre()方法,它返回一个由,分隔的字符串数组,如:piano,guitar,violin,.....

问题是,当我尝试搜索此术语时,例如:piano,guitar,然后它将返回例如piano,flute,guitar,但它将忽略只有一个类别的项目:如项目只有guitar标签作为类别的广告将不会显示。

请帮帮我吗?

@Override
public Filter getFilter() {
    Filter filter = new Filter() {
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            dataList = (List<ProductLocal>) results.values;
            notifyDataSetChanged();
        }
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            List<ProductLocal> filteredList = new ArrayList<>();

            for (int i = 0; i < dataListFilter.size(); i++) {
                ProductLocal dataNames = dataListFilter.get(i);

                int count = 0;
                String[] terms = constraint.toString().split(",");

                for (String term : terms) {

                    for (String str : dataNames.getGenre()) {

                        if (str.equals(term) || str.contains(term)) {
                            count++;
                            if (count == terms.length) {
                                filteredList.add(dataNames);
                            }
                        }
                    }
                }
            }

            results.count = filteredList.size();
            results.values = filteredList;
            return results;
        }

    };
    return filter;
}

0 个答案:

没有答案