【问题标题】:How to implement a CrossProcessCursor compatible generic custom CursorWrapper如何实现与 CrossProcessCursor 兼容的通用自定义 CursorWrapper
【发布时间】:2011-06-07 06:32:15
【问题描述】:

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

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

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

【问题讨论】:

    标签: android cursor wrapper cross-process


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-09-15
      • 2023-02-17
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 2011-11-26
      • 2012-08-11
      • 1970-01-01
      相关资源
      最近更新 更多