Skip to content

Instantly share code, notes, and snippets.

@ramzes642
Last active November 16, 2017 05:02
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ramzes642/5400792 to your computer and use it in GitHub Desktop.
Save ramzes642/5400792 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