Cursor.getType() for API Level <11

前端 未结 4 1047
北荒
北荒 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:57

    Expanding on Juan's answer, here is my replacement for the API 11 method Cursor.getType(int i) - for a cursor retuned by an SQL query

    public class DbCompat {
    
        protected static final int FIELD_TYPE_BLOB = 4;
        protected static final int FIELD_TYPE_FLOAT = 2;
        protected static final int FIELD_TYPE_INTEGER = 1;
        protected static final int FIELD_TYPE_NULL = 0;
        protected static final int FIELD_TYPE_STRING = 3;
    
        static int getType(Cursor cursor, int i) throws Exception {
            SQLiteCursor sqLiteCursor = (SQLiteCursor) cursor;
            CursorWindow cursorWindow = sqLiteCursor.getWindow();
            int pos = cursor.getPosition();
            int type = -1;
            if (cursorWindow.isNull(pos, i)) {
                type = FIELD_TYPE_NULL;
            } else if (cursorWindow.isLong(pos, i)) {
                type = FIELD_TYPE_INTEGER;
            } else if (cursorWindow.isFloat(pos, i)) {
                type = FIELD_TYPE_FLOAT;
            } else if (cursorWindow.isString(pos, i)) {
                type = FIELD_TYPE_STRING;
            } else if (cursorWindow.isBlob(pos, i)) {
                type = FIELD_TYPE_BLOB;
            }
    
            return type;
        }
    }
    

    gist: https://gist.github.com/kassim/c340cbfc5243db3a4826

提交回复
热议问题