I am trying to create an ExpandableListView dynamically in my application. The parent rows display the dates of the next 7 days and I retrieve the children from a web-service. But, fetching and displaying the children are not my problem. I am facing trouble in displaying the parent rows dynamically. I need to display the dates of the next 7 days in the parent rows.
I declared this empty array of strings globally.
private String[] parent = new String[7];
Then, inside onCreate(), I used this code to create the parent array contents.
Calendar cal = Calendar.getInstance();
String strdate = null;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
int i;
for(i=0;i<7;i++)
{
if (cal != null) {
strdate = sdf.format(cal.getTime());
}
parent[i] = strdate;
cal.add(Calendar.HOUR_OF_DAY, 24);
}
But, the activity gives a NullPointerException at MyExpandableListAdapter.getGroupView().
(Maybe because the parent array has not be initialized initially ?)
because the code works when I declare the parent array initally as
private String[] parent = new String[] {"","","".....};
My solution : Static Block was the answer to it. I put the code to create the parent array contents inside a static block instead of
onCreate()and it works like a charm now.