This is the class where I try to add elements from cursor to ArrayList.
public ArrayList<String> getArrayList()
{
int i;
sdb=this.getReadableDatabase();
c=null;
ArrayList<String> list=null;
try{
c=sdb.rawQuery("select * from Products", null);
c.moveToFirst();
if(c!=null)
{
list=new ArrayList<String>();
do
{
for(i=1;i<=c.getColumnCount();i++)
{
list.add(c.getString(c.getColumnIndex("ProductName")));
list.add(c.getString(c.getColumnIndex("ProduceType")));
list.add(c.getString(c.getColumnIndex("Company")));
list.add(c.getString(c.getColumnIndex("Price")));
list.add(c.getString(c.getColumnIndex("Quantity")));
}
}while(c.moveToNext());
}
else
System.out.println("c null");
}catch(Exception e)
{
e.printStackTrace();
//c=null;
}
/*finally{
if(c!=null && !c.isClosed())
{
c.close();
c=null;
}
close();
}*/
return list;
}
This is where I try to retrieve data from ArrayList and add them to expandablelistview.I want the 1st column of every row in cursor in the group view and the remaining columns of the corresponding row in child view.
private void loadData(){
db=new DatabaseHelper(this);
//db.open();
System.out.println("returned from db.open() in loadData");
ArrayList<String> al=db.getArrayList();
db.close();
int count=al.size();
int i,j;
try{
for(i=1;i<=count;i=i+5)
{
String pn=al.get(i);
for(j=i;j<=i+5;j++)
{
String inf=al.get(j);
addProduct(pn,inf);
}
}
}catch(StackOverflowError e)
{
e.printStackTrace();
}
}
I am going wrong with the for loop as of my knowledge.Logcat is showing the following error
07-04 02:44:14.747: E/CursorWindow(1881): Failed to read row 0, column -1 from a CursorWindow which has 3 rows, 5 columns.
Please tell me the correct usage of arraylist data retrieval.Thanks in advance.