android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为5

时间:2011-09-17 05:48:15

标签: android

请帮我解决这个问题

我的代码是

`final Cursor curr = dbhelper.getdatatomanagedata();
startManagingCursor(curr);
Integer colid = curr.getInt(0);
String colans = curr.getString(1);
objansMap = new HashMap<Integer, String>(); 
if (curr!=null)
{ 
 curr.moveToFirst();
 while(!curr.isAfterLast())
 {
    objansMap.put(colid, colans);
    curr.moveToNext();
 }}

1 个答案:

答案 0 :(得分:3)

您的代码必须为

final Cursor curr = dbhelper.getdatatomanagedata();
startManagingCursor(curr);
Integer colid;
String colans;
objansMap = new HashMap<Integer, String>(); 
if (curr!=null)
{ 
 curr.moveToFirst();
 while(!curr.isAfterLast())
 {  colid = curr.getInt(0);
    colans = curr.getString(1);
    objansMap.put(colid, colans);
    curr.moveToNext();
 }}

当游标返回其默认索引为-1时,您必须使用moveToFirst()移动到其第0个索引。

另一种将代码用作

的方法
if(cur.moveToFirst()){
        do{
            //YOUR CODE HERE 
           objansMap.put(curr.getInt(0), curr.getString(1));               
        }while(cur.moveToNext());
    }

快乐编码:)