/** Helper Method to create Demo Groups and Children */ private void initDemoData() { this.groups = new ArrayList<GroupClass>(); for (int groupCounter = 1; groupCounter <= 25; groupCounter++) { GroupClass group = new GroupClass("Group " + groupCounter); // Just a little trick to get a different number of children per group int numberOfChildren = (23 % groupCounter) + 2; for (int childCounter = 0; childCounter < numberOfChildren; childCounter++) { ChildClass child = new ChildClass( "Child " + childCounter, "Text for Child " + childCounter + " Text for Child " + childCounter + " Text for Child " + childCounter + " Text for Child " + childCounter + " Text for Child " + childCounter); group.addChild(child); } this.groups.add(group); } }
@Override public boolean onMenuItemSelected(int featureId, MenuItem item) { if (item.getItemId() == R.id.add_item) { GroupClass newGroup = new GroupClass("New Group " + System.currentTimeMillis()); newGroup.addChild(new ChildClass("New Child", "Child Text: " + System.currentTimeMillis())); this.groups.add(0, newGroup); this.adapter.notifyDataSetChanged(); return true; } else if (item.getItemId() == R.id.remove_item) { this.groups.remove(0); this.adapter.notifyDataSetChanged(); return true; } else { return super.onMenuItemSelected(featureId, item); } }
@Override public View getGroupView( int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ViewGroup item; if (convertView != null && convertView.getId() == R.id.group_item) { // We can reuse the View item = (ViewGroup) convertView; } else { // We create a new View item = (ViewGroup) inflater.inflate(R.layout.group_item_layout, parent, false); } GroupClass group = (GroupClass) getGroup(groupPosition); TextView nameView = (TextView) item.findViewById(R.id.group_name); nameView.setText(group.getName()); TextView sizeView = (TextView) item.findViewById(R.id.group_size); sizeView.setText("" + group.getChildren().size()); return item; }