1

我设法创建了一个工作CursorWrapper,但是当我想ContentProvider跨进程使用时卡住了。

这些帖子甚至展示了如何实现CrossProcessCursor接口,特别是硬且未记录fillWindow()的方法:

问题是我只能想到一个fillWindow()处理一个游标的实现,该游标的所有列都包含 Blob、字符串或 ...,但不能混合这些,因为现实世界的游标必然具有。这里真正的问题是缺少getType()函数(仅从 v11 开始存在),或者getRaw()/putRaw()只会复制二进制文件而不会抱怨。如何在返回的游标值不引起不必要的转换的情况下处理这个问题?

4

1 回答 1

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();
    }
}
于 2011-06-06T22:26:33.763 回答