开发者

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5

开发者 https://www.devze.com 2023-04-05 17:29 出处:网络
plz help me i face this problem my code is `final Cursor curr = dbhelper.getdatatomanagedata(); startManagingCursor(curr);

plz help me i face this problem

my code is

`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.moveT开发者_开发技巧oNext();
 }}


Your code must be as

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();
 }}

When cursor returns its default index is -1, and you must move to its 0th index by using moveToFirst() .

Another way to use your code as

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

Happy coding :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号