devin_lxs 2015-08-14 10:17 采纳率: 0%
浏览 1819

关于filter的问题请教下。

现在不知道错误在哪里。
现象:输入无效搜索内容,例如:3/5/9#!¥
列表闪显北京市,之后就正常了,而且并不是每次都出现,如图所示图片说明
代码应该是出现在这个函数里面
etInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
try {
if (!etInput.getText().toString().equals("")) {
if (cityList != null) {
adapter = new FilterAdapter(getActivity(),
cityList);
if (adapter != null) {
adapter.getFilter().filter(
etInput.getText().toString());
adapter.setFilterType(FilterAdapter.FILTER_BODY);

                            }
                        }
                    }
                    hideInputWindow();
                } catch (Exception e) {

                }
            }
            return false;
        }

    });

    然后下一个FilterAdapter.java
    public class FilterAdapter extends BaseAdapter implements Filterable {

public static final int FILTER_HEAD = 0;// 从头开始过滤
public static final int FILTER_FOOT = 1;// 从后开始过滤
public static final int FILTER_BODY = 2;// 任意过滤

private Context context;
private List<String> list = new ArrayList<String>();
private List<String> spylist = new ArrayList<String>();
private boolean needSort = false;// 是否需要排序
private ArrayFilter filter;// 过滤器
private int filterType = FILTER_BODY;// 过滤类别,默认从头开始过滤
private Event mFilterFinish = new Event(EventType.FILTER_CITY_FINISH);

public FilterAdapter(Context context, List<Map<String, Object>> obj) {
    this.context = context;
    for (int i = 0; i < obj.size(); i++) {
        this.list.add(obj.get(i).get("name").toString());
        this.spylist.add(obj.get(i).get("spy").toString());
    }
}

// 返回false后Item间的分割线消失
@Override
public boolean areAllItemsEnabled() {
    return false;
}

// 带有“-”的不可操作,通常和areAllItemsEnabled一起使用。
@Override
public boolean isEnabled(int position) {
    if( position >= list.size())
    {
        return false;
    }
    return !list.get(position).startsWith("-");// 此处根据需求灵活处理
}

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

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout layout;
    if (convertView == null) {
        layout = (LinearLayout) LayoutInflater.from(context).inflate(
                R.layout.mode_city_filter_list, parent, false);
    } else {
        layout = (LinearLayout) convertView;
    }

    TextView tv = (TextView)layout.findViewById(R.id.item_cityname);
    if(tv != null)
        tv.setText(list.get(position));
    return layout;
}

public List<String> getList() {
    return list;
}

public void setList(List<String> list) {
    this.list = list;
}

public boolean isNeedSort() {
    return needSort;
}

public void setNeedSort(boolean needSort) {
    this.needSort = needSort;
}

private List<String> dosort(List<String> list) {
    Collections.sort(list, comparator);
    return list;
}

private Comparator<String> comparator = new Comparator<String>() {
    private final Collator collator = Collator.getInstance();

    public int compare(String s1, String s2) {
        return collator.compare(s1, s2);
    }
};

@Override
public Filter getFilter() {
    // TODO Auto-generated method stub
    if (filter == null) {
        filter = new ArrayFilter();
    }
    return filter;
}

public int getFilterType() {
    return filterType;
}

public void setFilterType(int filterType) {
    this.filterType = filterType;
}

private class ArrayFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence prefix) {
        // TODO Auto-generated method stub
        FilterResults results = new FilterResults();
        if (prefix == null || prefix.length() == 0) {// 没有过滤符就不过滤
            // new
            // ArrayList<String>()表示让ListView一开始的时候什么都没有,而不是全部显示到ListView中
            // new ArrayList<String>(list)表示一开始就让Item全部显示到ListView中
            ArrayList<String> l = new ArrayList<String>(list);
            results.values = l;
            results.count = l.size();
        } else {

            String prefixString = prefix.toString().toUpperCase();
            Log.i("FilterAdapter", "prefixString = " + prefixString);

            final ArrayList<String> values = new ArrayList<String>(list);

            final int count = values.size();

            ArrayList<String> newValues = new ArrayList<String>(list);

            for (int i = 0; i < count; i++) {
                final String value = values.get(i);// 原始字符串
                final String valueText = value.toString().toLowerCase();
                final String spy = spylist.get(i).toString();
                Log.i("FilterAdapter", "value = " + value);
                Log.i("FilterAdapter", "valueText = " + valueText);
                Log.i("FilterAdapter", "spy = " + spy);

                // 匹配核心代码,需要自己实现,这里只提供了基本的过滤类型
                switch (filterType) {
                case FILTER_HEAD:
                    if (!valueText.startsWith(prefixString)) {// 过滤掉不需要的项
                        newValues.remove(value);
                        Log.i("FilterAdapter", "remove FILTER_HEAD " + value + ",lenght = " + newValues.size());
                    }
                    break;
                case FILTER_FOOT:
                    if (!valueText.endsWith(prefixString)) {// 过滤掉不需要的项
                        newValues.remove(value);
                        Log.i("FilterAdapter", "remove FILTER_FOOT " + value + ",lenght = " + newValues.size());
                    }
                    break;
                case FILTER_BODY:
                    if (spy.indexOf(prefixString) == -1 && valueText.indexOf(prefixString) == -1) {
                        newValues.remove(value);
                        Log.i("FilterAdapter", "remove FILTER_BODY " + value + ",lenght = " + newValues.size());
                    } /*else if (valueText.indexOf(prefixString) != -1) {// 过滤掉不需要的项
                        newValues.add(value);
                        Log.i("FilterAdapter", "add FILTER_BODY valueText " + value + ",lenght = " + newValues.size());
                    }*/
                    break;

                default:
                    break;
                }
                Log.i("FilterAdapter", "newValues.size() = " + newValues.size());
            }

            results.values = newValues;
            results.count = newValues.size();
            Log.i("FilterAdapter", "newValues.size() = " + newValues.size() +", results.count = " + results.count);
        }

        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        // TODO Auto-generated method stub
        list = (List<String>) results.values;
        /*if (results.count > 0) {
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }*/
        EventManager.trigger(mFilterFinish);
    }

}

}

  • 写回答

1条回答 默认 最新

  • devmiao 2015-08-14 23:10
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题