Skip to content

Instantly share code, notes, and snippets.

@erkobg
Forked from ramzes642/FilterCursorWrapper.java
Created December 19, 2015 19:56
Show Gist options
  • Save erkobg/02dd5e9a548e21f50af6 to your computer and use it in GitHub Desktop.
Save erkobg/02dd5e9a548e21f50af6 to your computer and use it in GitHub Desktop.
FilterCursorWrapper class creates a compatible cursor that uses a filter argument to quick search ListView in android. It can be useful when you need unicode case insensetive "like" search, but you don't want to rebuild android's sqlite with icu support.
private class FilterCursorWrapper extends CursorWrapper {
private String filter;
private int column;
private int[] index;
private int count=0;
private int pos=0;
public FilterCursorWrapper(Cursor cursor,String filter,int column) {
super(cursor);
this.filter = filter.toLowerCase();
this.column = column;
if (this.filter != "") {
this.count = super.getCount();
this.index = new int[this.count];
for (int i=0;i<this.count;i++) {
super.moveToPosition(i);
if (this.getString(this.column).toLowerCase().contains(this.filter))
this.index[this.pos++] = i;
}
this.count = this.pos;
this.pos = 0;
super.moveToFirst();
} else {
this.count = super.getCount();
this.index = new int[this.count];
for (int i=0;i<this.count;i++) {
this.index[i] = i;
}
}
}
@Override
public boolean move(int offset) {
return this.moveToPosition(this.pos+offset);
}
@Override
public boolean moveToNext() {
return this.moveToPosition(this.pos+1);
}
@Override
public boolean moveToPrevious() {
return this.moveToPosition(this.pos-1);
}
@Override
public boolean moveToFirst() {
return this.moveToPosition(0);
}
@Override
public boolean moveToLast() {
return this.moveToPosition(this.count-1);
}
@Override
public boolean moveToPosition(int position) {
if (position >= this.count || position < 0)
return false;
return super.moveToPosition(this.index[position]);
}
@Override
public int getCount() {
return this.count;
}
@Override
public int getPosition() {
return this.pos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment