过滤光标以正确的方式? [英] Filtering a cursor the right way?

查看:82
本文介绍了过滤光标以正确的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我需要过滤游标/ CursorAdapter的匹配在ListView特定的条件下,只显示行。我不想再次查询数据库所有的时间。我只是想过滤我从查询数据库得到的光标。

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.

我已经看到了这个问题: http://stackoverflow.com/questions/3027428

I have seen the question: http://stackoverflow.com/questions/3027428

但我不明白如何通过overwritting搬家的方法在我的CursorWrapper做过滤。一个例子是不错的。

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

非常感谢你。

推荐答案

更新:

我已经重写源和我的雇主取得了它作为开源软件:的https:// github上.COM /三叶草/ Android的filteredcursor

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

您不必重写CursorWrapper所有的移动方法,你就需要重写一堆,虽然由于光标接口的设计。让我们pretend要过滤掉行#7行光标2和#4,使扩展CursorWrapper一个类并重写这些方法如下所示:

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;
}

现在最有趣的部分是创建filterMap,这完全取决于你。

Now the interesting part is creating the filterMap, that's up to you.

这篇关于过滤光标以正确的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆