如何实现与CrossProcessCursor兼容的通用自定义CursorWrapper

时间:2011-06-06 22:02:23

标签: android cursor wrapper cross-process

我设法创建了一个有效的CursorWrapper,但是当我希望跨进程使用ContentProvider时,我会陷入困境。

这些帖子甚至展示了如何实施CrossProcessCursor界面,特别是硬文档和无文档fillWindow() 方法:

问题在于,我只能想到fillWindow()的实现,它处理的游标包含所有包含Blob或字符串的列,或者......,而不是这些列的混合< / strong>,作为真实世界的游标必然会有。这里真正的问题是缺少getType() 功能(仅存在于v11上),getRaw() / putRaw() 这只会复制二进制而不抱怨。如何在返回的游标值中处理此而不会产生不需要的转化

1 个答案:

答案 0 :(得分:0)

我现在已经按照以下方式实现了它,但它感觉不是正确的做法或健壮:

/**
 * Copy data from cursor to CursorWindow
 * @param position start position of data
 * @param window
 */
public void fillWindow(int position, CursorWindow window) {
    if (position < 0 || position > getCount()) {
        return;
    }
    window.acquireReference();
    try {
        int oldpos = this.getPosition();
        this.moveToPosition(position - 1);
        window.clear();
        window.setStartPosition(position);
        int columnNum = getColumnCount();
        window.setNumColumns(columnNum);
        while (moveToNext() && window.allocRow()) {            
            for (int i = 0; i < columnNum; i++) {

                //int type = getType(i);//only from v11 on

                try {
                    String field7 = getString(i);
                    if (field7 != null) {
                        try {
                                if (!window.putLong(new Long(field7), this.getPosition(), i)) {
                                    if (!window.putDouble(new Double(field7), this.getPosition(), i)) {
                                        if (!window.putString(field7, this.getPosition(), i)) {
                                            window.freeLastRow();
                                            break;
                                        }
                                    }
                                }
                            } catch (NumberFormatException e) {
                                try {
                                    if (!window.putDouble(new Double(field7), this.getPosition(), i)) {
                                        if (!window.putString(field7, this.getPosition(), i)) {
                                            window.freeLastRow();
                                            break;
                                        }
                                    }
                                } catch (NumberFormatException e1) {
                                    if (!window.putString(field7, this.getPosition(), i)) {
                                        window.freeLastRow();
                                        break;
                                    }
                                }
                            }
                    } else {
                        if (!window.putNull(this.getPosition(), i)) {
                            window.freeLastRow();
                            break;
                        }
                    }
                } catch (SQLiteException e7) {

                    try {
                        byte[] field1 = getBlob(i);
                        if (field1 != null) {
                            if (!window.putBlob(field1, this.getPosition(), i)) {
                                window.freeLastRow();
                                break;
                            }
                        } else {
                            if (!window.putNull(this.getPosition(), i)) {
                                window.freeLastRow();
                                break;
                            }
                        }
                    } catch (SQLiteException e1) {
                        throw e1;
                    }
                }
            }
        }

        this.moveToPosition(oldpos);
    } catch (IllegalStateException e){
        // simply ignore it
    } finally {
        window.releaseReference();
    }
}