首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何向SimpleCursorTreeAdapter添加“虚拟”/“影子”组?

如何向SimpleCursorTreeAdapter添加“虚拟”/“影子”组?
EN

Stack Overflow用户
提问于 2013-03-01 19:14:40
回答 1查看 290关注 0票数 4

我有一个关于SimpleCursorTreeAdapter的问题,我在我的项目中子类化了它。它目前通过ContentProvider与SQLite-DB通信。现在,我遇到了一个问题,我想插入一些后端ContentProvider/DB中不存在的“影子”或“虚拟”组。

我可以想象这可以通过两种方式实现--我只是不能弄清楚细节。我们要么找到在内部绘制和列出虚拟组的实际方法,要么实现一个行为类似于SQliteCursor的自定义游标,只是它在被查询时返回ColumnCount+1并为那个“虚拟”组附加一个“SimpleCursorTreeAdapter”元素...

有没有人已经做了这样的事情,或者可能知道如何解决这个不那么肮脏的黑客问题?

EN

回答 1

Stack Overflow用户

发布于 2013-03-02 01:06:05

...

或者您在适配器级别实现它,欺骗它,认为它所基于的Cursor有一个额外的行。我已经写了一个例子(一个简单的场景),但它需要改进:

代码语言:javascript
复制
public class ExtraGroupAdapter extends SimpleCursorTreeAdapter {

    private Cursor mExtraChildCursor;       

    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {
        // if groupCursor.getPosition() returns -1 we have to provide the Cursor for our fake group
    }

    @Override
    public int getGroupCount() {
        // 1 more
        return super.getGroupCount() + 1;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        if (groupPosition == 0) {
            if (getCursor() == null) {
                throw new IllegalStateException(
                        "this should only be called when the cursor is valid");
            }
            View v;
            if (convertView == null) {
                v = newGroupView(mContext, getCursor(), isExpanded, parent);
            } else {
                v = convertView;
            }
            // if it's position 0 move the group Cursor to position -1 to
            // let the bindGroupView method that is deals with our fake row
            getCursor().moveToPosition(-1);
            bindGroupView(v, mContext, getCursor(), isExpanded);
            return v;
        } else {
            return super.getGroupView(groupPosition - 1, isExpanded,
                    convertView, parent);
        }
    }

    @Override
    protected void bindGroupView(View view, Context context, Cursor cursor,
            boolean isExpanded) {
        if (cursor.getPosition() == -1) {
            // bind our fake row, unfortunately this must be done manually
        } else {
            super.bindGroupView(view, context, cursor, isExpanded);
        }
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        if (groupPosition == 0) {
            // replicate what the CursorTreeAdapter does for our fake group
            // position 0
            if (getCursor() == null) {
                throw new IllegalStateException(
                        "this should only be called when the cursor is valid");
            }
            View v;
            getCursor().moveToPosition(-1);
            mExtraChildCursor.moveToPosition(childPosition);
            if (convertView == null) {
                v = newChildView(mContext, mExtraChildCursor, isLastChild,
                        parent);
            } else {
                v = convertView;
            }
            bindChildView(v, mContext, mExtraChildCursor, isLastChild);
            return v;
        } else {
            // use the default implementation but offset the Cursor's
            // current position
            return super.getChildView(groupPosition - 1, childPosition,
                    isLastChild, convertView, parent);
        }
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (groupPosition == 0) {
            // implement the trick
            if (mExtraChildCursor == null) {
                getCursor().moveToPosition(-1); // in the getChildrenCursor
                                                // a Cursor position of -1
                                                // means it is the fake row
                mExtraChildCursor = getChildrenCursor(getCursor());
                return 0;
            } else {
                return mExtraChildCursor.getCount();
            }
        }
        return super.getChildrenCount(groupPosition);
    }

    @Override
    public void setChildrenCursor(int groupPosition, Cursor childrenCursor) {
        if (groupPosition == 0) {
            // hold a reference to the extra Cursor
            mExtraChildCursor = childrenCursor;
            notifyDataSetChanged();
        } else {
            super.setChildrenCursor(groupPosition, childrenCursor);
        }
    }

}

我应该扩展CursorTreeAdapter,因为SimpleCursorTreeAdapter是为简单场景而设计的。我所写的是放置在位置0的伪行(但如果仔细计算,可以使用不同的位置)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15156374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档