android sqlist中游标下标越界问题解决方案
在使用android的sqlist时,出现了以下错误
android.database.CursorIndexOutOfBoundsException: Index 14 requested, with a size of 14
经过排除,确定了错误位置
错误代码为
publicArrayList<receiveContext>query(Stringtable_name,String[]arg){SQLiteDatabasedb=getWritableDatabase();Cursorc=db.query(table_name,null,poster+"=?",arg,null,null,updateTime,null);//查询并获得游标ArrayList<receiveContext>l=newArrayList<receiveContext>();if(c.moveToFirst()){//判断游标是否为空for(inti=0;i<c.getCount();i++){c.moveToPosition(i);//正确语句//c.move(i);//错误语句StringgetContext="";intgetStyle=0;StringgetTime="";getContext=c.getString(c.getColumnIndex(context));getStyle=c.getInt(c.getColumnIndex(style));getTime=c.getString(c.getColumnIndex(updateTime));receiveContextrc=newreceiveContext(getContext,getStyle,getTime);l.add(rc);}}c.close();db.close();returnl;}
这是我写的一个数据库查询的方法,越界原因是使用了方法c.move(i);这个方法我估计(有待考证)应该是当前指针+i,所以会导致越界。正确的方法应该是
c.moveToPosition(i);
这是跳到第i个位置。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。