简体   繁体   English

自定义android.widget.Filter

[英]Custom android.widget.Filter

I've implemented this solution to filter my ListView but I'm using Spanish words and as you know they could have accents (for example: Árbol, Avión, etc.) and I'd like to filter my ListView with insensitive case, it is if I write avion, it get me Avión as a possible result because it disregard accents (á = a and a = Á). 我已经实现了此解决方案来过滤我的ListView但是我使用的是西班牙语单词,并且您知道它们可能带有重音符号(例如:Árbol,Avión等),并且我希望使用不区分大小写的过滤器来过滤ListView ,如果我写avion,它可能会给我Avión,因为它不考虑重音(á= a和a =Á)。 In order to do this I've used replaceAll java method from java.lang.String class but I makes my filter so slow. 为了做到这一点,我使用了java.lang.String类中的replaceAll java方法,但是我的过滤器太慢了。 Do you know alternatives to do this? 您知道替代方法吗?

I did same staff with a class which provides displayed string and converted string: 我为提供显示的字符串和转换后的字符串的类做过相同的工作人员:

public class Element {
protected String dispString;
protected String convString;
public Element(String dispString)
    this.dispString=dispString;
    this.convString=getConvString(convString);
}

public String getDispString() {
    return dispString;
}
public String toString() {
    return convString;
}

public static String getConvString(val) {
if(val==null)
    return null;
// Do your conversion here
}

Instead of a string array I used an array of the Element class instances. 我使用了一个Element类实例的数组来代替字符串数组。 But I had to use ArrayAdapter's getView() method to display dispString instead of implicit toString() string: 但是我不得不使用ArrayAdapter的getView()方法来显示dispString而不是隐式toString()字符串:

adapter=new ArrayAdapter<Element>(this, R.layout.layout_gpslocator_g_owners_child, owners) {
    public View getView(int position, View convertView, ViewGroup parent) {
        View childView=super.getView(position, convertView, parent);
        ((TextView)childView.findViewById(<child TextView id>)).setText(this.getItem(position).getDispString());
        return childView;
    }
};

Element.toString() is then used for filtering. 然后,使用Element.toString()进行过滤。 Element.getDispString() is used for displayed string. Element.getDispString()用于显示的字符串。 And during initialization of the array, all conversion is pre-cached and there is no need for list view's data conversion during filtering. 并且在数组初始化期间,所有转换都已预先缓存,并且在过滤过程中不需要列表视图的数据转换。 You have to call Element.getConvString(val) for adapter.getFilter().filter() calling when a text is changed. 更改文本后,必须调用Element.getConvString(val)进行adapter.getFilter().filter()调用。

Hope this helped. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM