本文介绍了Cursor.getType()的API级别< 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查询CallLog内容提供商和需要检测列类型

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

在蜂窝和更高版本(API等级11+),您可以通过调用方法得到一个列preferred数据类型 Cursor.getType(INT参数:columnIndex)返回以下类型之一:

In Honeycomb and newer (API Level 11+) you can get a columns preferred data type by calling the method Cursor.getType(int columnIndex) which returns one of the following types:

  • FIELD_TYPE_NULL(0)
  • FIELD_TYPE_INTEGER(1)
  • FIELD_TYPE_FLOAT(2)
  • FIELD_TYPE_STRING(3)
  • FIELD_TYPE_BLOB(4)

我怎样才能做到这一点的pre-蜂窝< 11台设备

How can I accomplish this on pre-Honeycomb <11 devices?

我已经试过如下:

for ( int i = 0; i < cursor.getColumnCount(); i++ ) {

    int columnType = -1;
    try {
        cursor.getInt( i );
        columnType = Cursor.FIELD_TYPE_INTEGER;

    } catch ( Exception ignore ) {

        try {
            cursor.getString( i );
            columnType = Cursor.FIELD_TYPE_STRING;

        } catch ( Exception ignore1 ) {

            try {
                cursor.getFloat( i );
                columnType = Cursor.FIELD_TYPE_FLOAT;

            } catch ( Exception ignore2 ) {

                try {
                  cursor.getBlob( i );
                  columnType = Cursor.FIELD_TYPE_BLOB;

                } catch ( Exception ignore3 ) {

                     columnType = Cursor.FIELD_TYPE_NULL;
                }
           }
       }
   }

}

但是,没有异常。该数据总是浇铸在要检查的第一类型,在这种情况下,调用getInt()。这意味着,我得到了正确的值,如果列类型是整数但是 0 对于所有其他类型。

However, no exception is thrown. The data is always casted in the first type you are checking for, in this case getInt(). That means, I get the correct values if the column type is Integer but a 0 for all other types.

为什么我不看检查什么类型存储的文档中?列不同而有所不同设备制造商,而不是所有的人都记录在案,看到了这个问题:How处理制造商依赖于ContentProviders差异?

Why am I not looking in the documentation to check what type is stored?The columns differ depending on the device manufacturer and not all of them are documented, see this question: How to handle manufacturer-dependent differences in ContentProviders?

任何想法?

推荐答案

您可以使用此code时,光标定位在一个有效行:

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";
    }
}

需要注意的是Cursor.FIELD_TYPE_ *常量的定义从蜂巢开始。

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

这篇关于Cursor.getType()的API级别&LT; 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 00:34