Cursor.getType() for API Level <11

前端 未结 4 1042
北荒
北荒 2020-12-10 01:00

I\'m querying the CallLog content provider and need to detect the column types.

In Honeycomb and newer (API Level 11+) you can get a columns preferred data type by c

4条回答
  •  感情败类
    2020-12-10 01:40

    You can use this code when cursor is positioned in a valid row:

    CursorWrapper cw = (CursorWrapper)cursor;
    
    Class cursorWrapper = CursorWrapper.class;
    Field mCursor = cursorWrapper.getDeclaredField("mCursor");
    mCursor.setAccessible(true);
    AbstractWindowedCursor abstractWindowedCursor = (AbstractWindowedCursor)mCursor.get(cw);
    CursorWindow cursorWindow = abstractWindowedCursor.getWindow();
    int pos = abstractWindowedCursor.getPosition();
    for ( int i = 0; i < cursor.getColumnCount(); i++ ) {
        String type = null;
        if (cursorWindow.isNull(pos, i)) {
            type = "Cursor.FIELD_TYPE_NULL";
        } else if (cursorWindow.isLong(pos, i)) {
            type = "Cursor.FIELD_TYPE_INTEGER";
        } else if (cursorWindow.isFloat(pos, i)) {
            type = "Cursor.FIELD_TYPE_FLOAT";
        } else if (cursorWindow.isString(pos, i)) {
            type = "Cursor.FIELD_TYPE_STRING";
        } else if (cursorWindow.isBlob(pos, i)) {
            type = "Cursor.FIELD_TYPE_BLOB";
        }
    }
    

    Note that Cursor.FIELD_TYPE_* constant values are defined starting from HONEYCOMB.

提交回复
热议问题