【解决】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();

更多相关文章

  1. 【安卓】关于 Android 默认字体以及对比微软雅黑字体
  2. 微软发布 mircosft remote desktop for android
  3. Android连接SQLServer详细教程(数据库+服务器+客户端),并在微软Azur
  4. 专利敲诈——微软对Android痛下杀手的背后
  5. Android——SpannableString上标,下标垂直对齐
  6. 【更新】Google 与微软开始口水战
  7. 微软一年通过Android获得几十亿美元收入,没错,是微软!
  8. 不仅是微软和诺基亚,谁都无法 fork Android,因为它就没法 fork
  9. Android:微软的金钱机器(更新)

随机推荐

  1. Android™ 2.1 android.R.drawable Icon
  2. Android(安卓)Studio开发环境的搭建与And
  3. Android 源码网站
  4. android 4.0 HttpURLConnection. getInpu
  5. 第一章:hello,Android
  6. android api文档在线
  7. understanding android build layer · D
  8. Android布局文件中的属性含义
  9. Android Treble 简介
  10. ch013 Android ScrollView与ListView