vicmonmena
vicmonmena

Reputation: 41

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 = Á). In order to do this I've used replaceAll java method from java.lang.String class but I makes my filter so slow. Do you know alternatives to do this?

Upvotes: 0

Views: 304

Answers (1)

Ivo Pavlik
Ivo Pavlik

Reputation: 297

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. But I had to use ArrayAdapter's getView() method to display dispString instead of implicit toString() string:

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.getDispString() is used for displayed string. 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.

Hope this helped.

Upvotes: 1

Related Questions