Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6538879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:47:21+00:00 2026-05-25T10:47:21+00:00

At the moment I need to filter a Cursor/CursorAdapter to only show rows that

  • 0

At the moment I need to filter a Cursor/CursorAdapter to only show rows that match a specific condition in the ListView. I don’t want to requery the db all the time. I just want to filter the Cursor I got from querying the DB.

I have seen the question: Filter rows from Cursor so they don't show up in ListView

But I don’t understand how to do the filtering by overwritting the “move” methods in my CursorWrapper. An example would be nice.

Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:47:22+00:00Added an answer on May 25, 2026 at 10:47 am

    UPDATE:

    I have rewritten the source and my employer has made it available as open source software: https://github.com/clover/android-filteredcursor

    You don’t need to override all the move methods in CursorWrapper, you do need to override a bunch though due to the design of the Cursor interface. Let’s pretend you want to filter out row #2 and #4 of a 7 row cursor, make a class that extends CursorWrapper and override these methods like 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 the end of the cursor
        final int count = getCount();
        if (position >= count) {
            mPos = count;
            return false;
        }
    
        // Make sure position isn't before the beginning of the cursor
        if (position < 0) {
            mPos = -1;
            return false;
        }
    
        final int realPosition = filterMap[position];
    
        // When moving to an empty position, just pretend we did it
        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 the interesting part is creating the filterMap, that’s up to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running into a bit of dilemma at the moment in that I need
This is a general question, but I'll explain my specific need at the moment:
I need a list of URLs to apply filters to and at the moment
I am doing a project at the moment and I need an efficient method
I need to attach a file with mailx but at the moment I am
I need to use sendmail from Macs in an office. At the moment, I
I need to decide which configuration framework to use. At the moment I am
I seem to be blind at the moment, so I need to ask here.
Got a c#.net app which I need to modify. The query at the moment
At moment I want to implement picture upload without using any plug-ins. My upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.