Piyush
Piyush

Reputation: 3071

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

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

Upvotes: 1

Views: 1138

Answers (1)

Pankaj Kumar
Pankaj Kumar

Reputation: 82978

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 :)

Upvotes: 3

Related Questions