android - Filtering a cursor the right way? -


at moment need filter cursor/cursoradapter show rows match specific condition in listview. don't want requery db time. want filter cursor got querying db.

i have seen question: filter rows cursor don't show in listview

but don't understand how filtering overwritting "move" methods in cursorwrapper. example nice.

thank much.

update:

i have rewritten source , employer has made available open source software: https://github.com/clover/android-filteredcursor

you don't need override move methods in cursorwrapper, need override bunch though due design of cursor interface. let's pretend want filter out row #2 , #4 of 7 row cursor, make class extends cursorwrapper , override these methods so:

private int[] filtermap = new int[] { 0, 1, 3, 5, 6 }; private int mpos = -1;  @override public int getcount() { return filtermap.length }  @override public boolean movetoposition(int position) {     // make sure position isn't past end of cursor     final int count = getcount();     if (position >= count) {         mpos = count;         return false;     }      // make sure position isn't before beginning of cursor     if (position < 0) {         mpos = -1;         return false;     }      final int realposition = filtermap[position];      // when moving empty position, pretend did     boolean moved = realposition == -1 ? true : super.movetoposition(realposition);     if (moved) {         mpos = position;     } else {         mpos = -1;     }     return moved; }  @override public final boolean move(int offset) {     return movetoposition(mpos + offset); }  @override public final boolean movetofirst() {     return movetoposition(0); }  @override public final boolean movetolast() {     return movetoposition(getcount() - 1); }  @override public final boolean movetonext() {     return movetoposition(mpos + 1); }  @override public final boolean movetoprevious() {     return movetoposition(mpos - 1); }  @override public final boolean isfirst() {     return mpos == 0 && getcount() != 0; }  @override public final boolean islast() {     int cnt = getcount();     return mpos == (cnt - 1) && cnt != 0; }  @override public final boolean isbeforefirst() {     if (getcount() == 0) {         return true;     }     return mpos == -1; }  @override public final boolean isafterlast() {     if (getcount() == 0) {         return true;     }     return mpos == getcount(); }  @override public int getposition() {     return mpos; } 

now interesting part creating filtermap, that's you.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -