I have an SQLite database with multiple columns, and one of these columns is a date. I want to populate the data from the database to an expandable listview, with the dates as the headings. So for all the rows that share the same date value, I want to display them as children to that specific date.
I did it using arraylist to store the data from the database. I got the group headings showing up but I get null pointer exception when I try to expand. The error comes from the getChildView method.
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
public Object getChild(int groupPosition, int childPosition) {
return infoList.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
RelativeLayout infoLayout = (RelativeLayout) findViewById(R.id.infoLayout);
String exercise = ((ArrayList<Object>) getChild(groupPosition, childPosition)).get(0).toString();
String weight = ((ArrayList<Object>) getChild(groupPosition, childPosition)).get(1).toString();
String reps = ((ArrayList<Object>) getChild(groupPosition, childPosition)).get(2).toString();
((TextView) infoLayout.findViewById(R.id.exercise)).setText(exercise);
((TextView) infoLayout.findViewById(R.id.weight)).setText(weight);
((TextView) infoLayout.findViewById(R.id.reps)).setText(reps);
return infoLayout;
}
public int getChildrenCount(int groupPosition) {
return infoList.get(groupPosition).size();
}
public Object getGroup(int groupPosition) {
return dateList.get(groupPosition);
}
public int getGroupCount() {
return dateList.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView dateView = new TextView(getApplicationContext());
dateView.setText(getGroup(groupPosition).toString());
return dateView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
And this is how I populated the arraylists. The arraylist containing the children are nested twice: the innermost contains 3 objects to be displayed on the same row, and the "middle" one groups the innermost arraylists together so that they appear under the correct groupings for the expandable listview.
private DatabaseMethods dbm;
private ArrayList<String> dateList = new ArrayList<String>();
private ArrayList<ArrayList<ArrayList<Object>>> infoList = new ArrayList<ArrayList<ArrayList<Object>>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats);
dbm = new DatabaseMethods(this);
dbm.read();
Cursor dates = dbm.fetchGroup();
int DATES_INDEX = dates.getColumnIndexOrThrow("date");
while (dates.moveToNext()) {
String date = dates.getString(DATES_INDEX);
if (!dateList.contains(date)) {
dateList.add(date);
}
}
for (String date : dateList) {
Cursor info = dbm.fetchChildren(date);
int EXERCISE_INDEX = info.getColumnIndexOrThrow("exercise");
int WEIGHT_INDEX = info.getColumnIndexOrThrow("weight");
int REPS_INDEX = info.getColumnIndexOrThrow("reps");
ArrayList<ArrayList<Object>> secondInnerInfoList = new ArrayList<ArrayList<Object>>();
while (info.moveToNext()) {
String exercise = info.getString(EXERCISE_INDEX);
int weight = info.getInt(WEIGHT_INDEX);
int reps = info.getInt(REPS_INDEX);
ArrayList<Object> thirdInnerInfoList = new ArrayList<Object>();
thirdInnerInfoList.add(exercise);
thirdInnerInfoList.add(weight);
thirdInnerInfoList.add(reps);
secondInnerInfoList.add(thirdInnerInfoList);
infoList.add(secondInnerInfoList);
}
}