我需要将Cursor(sqliteCursor)从服务传递到API 10上的应用程序,并且很难找到一个体面(和快速)的解决方案.
我见过CursorWindow类.这是Parcelable但我无法在API 10上实例化此类以使用sqliteCursor.fillWindow(),因为它没有有效的构造函数.不推荐使用CursorWindow(boolean).
即使我使用来自sqliteCursor的数据获得了CursorWindow实例,如何将此窗口复制到新的Cursor中?我应该使用什么Cursor实现呢?我看不到扩展AbstractWindowedCursor的可用Cursor.
谢谢你的时间!
最佳答案
我实现了一个ParcelableCursor类,它实现了CrossProcessCursor和Parcelable接口.如果有人有兴趣,我会发布它.尚未支持/实现某些操作,以及使用自定义BijectiveMap(这很容易实现).
/**
* Prefer ParcelableCursorForIntent instead.
IoUs() {
final int oldPos = this.curRow--;
if (isBeforeFirst()) {
this.curRow = -1;
return false;
}
return onMove(oldPos,this.curRow);
}
@Override
public boolean onMove(final int oldPosition,final int newPosition) {
// Don't forget to set curRow = -1 if this method returns false
return true;
}
/** Restoring this object from a Parcel */
public void readFromParcel(final Parcel in) {
this.numColumns = in.readInt();
this.colNames = in.readParcelable(ClassLoaderHelper.getClassLoader());
this.curRow = in.readInt();
this.closed = (in.readByte() == 1);
// Closes the cursor before create a new cursor.
if (window != null) {
window.close();
}
this.window = CursorWindow.newFromParcel(in);
}
/** Not supported */
@Override
public void registerContentObserver(final ContentObserver observer) {
// Does nothing
}
/** Not supported */
@Override
public void registerDataSetObserver(final DataSetObserver observer) {
// Does nothing
}
/** Deprecated,not supported */
@Override
public boolean requery() {
return false;
}
/** Not supported */
@Override
public Bundle respond(final Bundle extras) {
// Does nothing
return null;
}
/** Sets this cursor from another windowed Cursor */
public void setFromCursor(final AbstractWindowedCursor cursor) throws CursorIndexOutOfBoundsException,IllegalStateException {
// Reset number of columns
this.numColumns = 0;
// Set column names
final String[] colNames = cursor.getColumnNames();
if (colNames != null) {
for (final String col : colNames) {
addColumn(col);
}
}
// Fill window
this.window.clear();
this.window.setNumColumns(this.numColumns);
cursor.fillWindow(0,this.window);
moveToPosition(-1);
}
/** Sets this cursor from another windowed Cursor */
public void setFromCursor(final MatrixCursor cursor) throws CursorIndexOutOfBoundsException,IllegalStateException{
// Reset number of columns
this.numColumns = 0;
// Set column names
final String[] colNames = cursor.getColumnNames();
if (colNames != null) {
for (final String col : colNames) {
addColumn(col);
}
}
// Fill window
this.window.clear();
this.window.setNumColumns(this.numColumns);
cursor.fillWindow(0,this.window);
moveToPosition(-1);
}
/** Sets this cursor using a CursorWindow data */
public void setFromWindow(final CursorWindow window) {
CursorHelper.copyCursorWindow(0,window,this.window);
this.numColumns = CursorHelper.getCursorWindowNumCols(window);
moveToPosition(-1);
}
/** Not supported */
@Override
public void setNotificationUri(final ContentResolver cr,final Uri uri) {
// Does nothing
}
/** Not supported */
@Override
public void unregisterContentObserver(final ContentObserver observer) {
// Does nothing
}
/** Not supported */
@Override
public void unregisterDataSetObserver(final DataSetObserver observer) {
// Does nothing
}
@Override
public void writeToParcel(final Parcel out,final int flags) {
out.writeInt(this.numColumns);
out.writeParcelable((Parcelable) this.colNames,0);
out.writeInt(this.curRow);
out.writeByte(this.closed ? (byte) 1 : 0);
this.window.writeToParcel(out,flags);
}
}
仍在寻找更标准的方法来做到这一点.任何信息将非常感激!
编辑:这通过了很少的测试,所以在使用它之前测试它.
编辑2:事实上,它充满了错误……我将很快用更少的错误版本更新.
EDIT3:使用我们正在使用的工作光标更新一年.