I am fetching data from cursor. Using this cursor i am populating the text for my radio button. While displaying the activity it works fine but when i try to select the radio button, i again try to access my cursor and at that time i get the cursor out of bounds exception. Please help me out.
Cursor cur=db.getForcemodLevelForMenuitem(mnuItmID);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
cnt=1;
//Level 1
if(cur.getInt(0)==1)
{
LinearLayout level1Layout=(LinearLayout)findViewById(R.id.level1Layout);
level1Layout.setVisibility(View.VISIBLE);
//ArrayList menuitemForcemodMapsLevel1=db.GetBaseObjectListFromDBWhere("MenuitemForcemodMapping", "MenuitemID= "+mnuItmID+ " and Level="+1);
menuitemForcemodMapsLevel1=db.getForcemodDetailsForMenuitem(mnuItmID,1);
RadioGroup level1_rg=(RadioGroup) findViewById(R.id.level1_rg);
RadioButton[] rblevel1=new RadioButton[menuitemForcemodMapsLevel1.getCount()];
menuitemForcemodMapsLevel1.moveToFirst();
int i=0;
while (menuitemForcemodMapsLevel1.isAfterLast() == false) {
rblevel1[i] =new RadioButton(AddPopupMenuItem.this);
rblevel1[i].setText(menuitemForcemodMapsLevel1.getString(1).substring(0));
rblevel1[i].setTextColor(R.color.black);
level1_rg.addView(rblevel1[i]);
rblevel1[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
menuitemForcemodMapsLevel1.moveToPosition(i);
forceModNameList.add(menuitemForcemodMapsLevel1.getString(1).substring(0));
}
});
i++;
menuitemForcemodMapsLevel1.moveToNext();
}
menuitemForcemodMapsLevel1.close();
rblevel1=null;
}
}
cur.moveToNext();
}
cur.close();
db.close();
}
}
I am getting error at this line
:forceModNameList.add(menuitemForcemodMapsLevel1.getString(1).substring(0));
Thanks ,
Neha
You already retreive the 1st element at :
so the cursor is moved ahead. And then you are asking again the same thing to retrieve. Cursor can’t move backwards, it moves only forward by default. So you are getting this error.
Why don’t you save the value in a
Stringand can thus use again without the need of retreiving it again.I beleive this will simplify your code and also solve the problem. Try out.