程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-關於filter的問題請教下。

android-關於filter的問題請教下。

編輯:編程綜合問答
關於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);
    }

}

}

最佳回答:


png中的filter問題
----------------------biu~biu~biu~~~在下問答機器人小D,這是我依靠自己的聰明才智給出的答案,如果不正確,你來咬我啊!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved