Search code examples
androidsqliteandroid-cursor

How to hide specific rows of a Cursor in android


I have a cursor, and it got, lets say, 40 rows, I want to hide some rows when the user check a checkbox.

one way is run the query again on the cursor, but it doesn't help me because the condition is done by Java (calculate balance, with many logic).

I need something that will get the current row, and return if it can be show or not.

any help will be appreciated.


Solution

  • i inherit CursorWrapper and override some of the methods, here is the code:

    public class SelectableCursorWrapper extends CursorWrapper {
    
    private HashSet<Integer> mWhichShow;
    
    public SelectableCursorWrapper(Cursor cursor, HashSet<Integer> whichToShow) {
        super(cursor);
    
        mWhichShow = whichToShow;
    }
    
    @Override
    public int getCount() {
        return mWhichShow.size();
    }
    
    @Override
    public boolean moveToPosition(int position) {
        if (position >= super.getCount()) {
            return false;
        }
    
        if (mWhichShow.contains(position)) {
            return super.moveToPosition(position);
        } else {
            // Recursion on this method to move to the next element properly
            return this.moveToPosition(position + 1);
        }
    }
    }
    

    thanks for anyone that try to help!