简体   繁体   English

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

[英]How to implement a CrossProcessCursor compatible generic custom CursorWrapper

I manage to create a working CursorWrapper , but get stuck when I want to use my ContentProvider across processes.我设法创建了一个有效的CursorWrapper ,但是当我想跨进程使用我的ContentProvider时卡住了。

These posts even show how to implement the CrossProcessCursor interface, notably the hard and undocumented fillWindow() method:这些帖子甚至展示了如何实现CrossProcessCursor接口,特别是硬且未记录的fillWindow()方法:

The problem is that I can think only of an implementation of fillWindow() that deals with a cursor with either all columns containing Blobs, or Strings, or..., but not a mix of those , as a real-world cursor is bound to have.问题是我只能想到一个fillWindow()的实现,它处理 cursor 的所有列都包含 Blob 或字符串,或者......,但不是这些的混合,因为现实世界的 cursor 是绑定的具有。 The real issue here is a lack of a getType() function (exists only from v11 on), or a getRaw() / putRaw() that would just copy binary without complaining.这里真正的问题是缺少getType() function (仅从 v11 开始存在),或者只是复制二进制而不抱怨的getRaw() / putRaw() How do you deal with this without incurring unwanted conversions in your returned cursor values?您如何处理此问题,而不会在返回的 cursor 值中产生不必要的转换

I've implemented it as follows for now, but it does not feel like the right thing to do or robust:我现在已经按如下方式实现了它,但它感觉不是正确的做法或健壮:

/**
 * 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM