首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >API级别<11的Cursor.getType()

API级别<11的Cursor.getType()
EN

Stack Overflow用户
提问于 2012-07-25 20:39:43
回答 4查看 8.9K关注 0票数 24

我正在查询CallLog内容提供程序,需要检测列类型。

在蜂窝和更新(API级11+)中,可以通过调用Cursor.getType(int columnIndex)方法获得列首选数据类型,该方法返回以下类型之一:

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

如何在蜂窝前<11设备上完成这一任务?

我试过以下几种方法:

代码语言:javascript
复制
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()。这意味着,如果列类型为Integer,但对于所有其他类型是,则获得正确的值。

为什么我不查看文档来检查存储的类型?列因设备制造商而异,并非所有列都有文档记录,请参阅以下问题:How to handle manufacturer-dependent differences in ContentProviders?

有什么想法吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-09-24 00:45:15

当游标定位在有效行中时,可以使用此代码:

代码语言:javascript
复制
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_*常量是从蜂窝开始定义的。

票数 10
EN

Stack Overflow用户

发布于 2013-12-19 15:31:04

扩展Juan的答案,下面是我对API 11方法Cursor.getType(int )的替代--用于由SQL查询重新调整的游标。

代码语言:javascript
复制
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;
    }
}

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

票数 12
EN

Stack Overflow用户

发布于 2012-08-23 14:34:05

有些事情可能会奏效:http://developer.android.com/reference/android/database/DatabaseUtils.html cursorRowToContentValues

将复制ContentValues对象中的行。然后,您可以调用ContentValues.get(),这为您提供了一个对象。然后,您可以查看该对象的类。

编辑

根据DatabaseUtils的源代码,对象要么是blobs,要么是String。

编辑2

但是,如果您的游标是WindowedCursor,则它有方法来了解对象类型。(isBlob,isString,isLong.)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11658239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档