我正在查询CallLog内容提供程序,需要检测列类型。
在蜂窝和更新(API级11+)中,可以通过调用Cursor.getType(int columnIndex)方法获得列首选数据类型,该方法返回以下类型之一:
如何在蜂窝前<11设备上完成这一任务?
我试过以下几种方法:
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?
有什么想法吗?
发布于 2013-09-24 00:45:15
当游标定位在有效行中时,可以使用此代码:
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_*常量是从蜂窝开始定义的。
发布于 2013-12-19 15:31:04
扩展Juan的答案,下面是我对API 11方法Cursor.getType(int )的替代--用于由SQL查询重新调整的游标。
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;
}
}发布于 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.)
https://stackoverflow.com/questions/11658239
复制相似问题