android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 0
16lz
2021-01-23
【解决】android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 0
2012-02-18 21:57:34|分类:Android开发|标签:androidcursor解决|字号订阅
android 中数据库处理,特别是使用cursor时,注意初始位置,好像是从下标为-1的地方开始的,也就是说一次查询中,返回给cursor查询结果时,不能够马上从cursor中提取值。 比如,下面的代码会返回错误,android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 0:int score = ((Cursor)getReadableDatabase().query(TABLE_NAME, new String[]{"learned"}, "_id=?", new String[]{""+id}, null, null, null,"1")).getInt(0);
正确的用法:
Cursor cursor = getReadableDatabase().query(TABLE_NAME, new String[]{"learned"}, "_id=?", new String[]{""+id}, null, null, null,"1");
int learned=0;
if(cursor.moveToFirst()){
score= cursor.getInt(0);
}
cursor.close();
更多相关文章
- 【安卓】关于 Android 默认字体以及对比微软雅黑字体
- 微软发布 mircosft remote desktop for android
- Android连接SQLServer详细教程(数据库+服务器+客户端),并在微软Azur
- 专利敲诈——微软对Android痛下杀手的背后
- Android——SpannableString上标,下标垂直对齐
- 【更新】Google 与微软开始口水战
- 微软一年通过Android获得几十亿美元收入,没错,是微软!
- 不仅是微软和诺基亚,谁都无法 fork Android,因为它就没法 fork
- Android:微软的金钱机器(更新)