1. public class MyTagExpandableListAdapter extends BaseExpandableListAdapter {
  2.  
  3. private static final int CHILD_TYPE_COUNT = 2;
  4. private Context _context;
  5. private List<String> _listDataHeader; // header titles
  6. // child data in format of header title, child title
  7. private HashMap<String, List<String>> _listDataChild;
  8.  
  9. public MyTagExpandableListAdapter(Context context, List<String> listDataHeader,
  10. HashMap<String, List<String>> listChildData) {
  11. this._context = context;
  12. this._listDataHeader = listDataHeader;
  13. this._listDataChild = listChildData;
  14. }
  15.  
  16. @Override
  17. public Object getChild(int groupPosition, int childPosititon) {
  18. return this._listDataChild.get(this._listDataHeader.get(groupPosition))
  19. .get(childPosititon);
  20. }
  21.  
  22. @Override
  23. public long getChildId(int groupPosition, int childPosition) {
  24. return childPosition;
  25. }
  26.  
  27. @Override
  28. public int getChildType(int groupPosition, int childPosition) {
  29. String s = (String)getChild(groupPosition, childPosition);
  30. if(s.isEmpty())
  31. return 0;
  32. else
  33. return 1;
  34. }
  35.  
  36. @Override
  37. public int getChildTypeCount() {
  38. return CHILD_TYPE_COUNT;
  39. }
  40.  
  41. @Override
  42. public View getChildView(int groupPosition, final int childPosition,
  43. boolean isLastChild, View convertView, ViewGroup parent) {
  44.  
  45. final String childText = (String) getChild(groupPosition, childPosition);
  46.  
  47. EmptyViewHolder emptyViewHolder = null;
  48. PopulatedViewHolder populatedViewHolder = null;
  49.  
  50. int type = getChildType(groupPosition, childPosition);
  51.  
  52. if (convertView == null || (type == 0 && (convertView.getTag()) instanceof PopulatedViewHolder)
  53. || (type == 1 && (convertView.getTag()) instanceof EmptyViewHolder)) {
  54.  
  55. LayoutInflater infalInflater = (LayoutInflater) this._context
  56. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  57.  
  58. switch(type){
  59. case 0:
  60. //type is empty
  61. emptyViewHolder = new EmptyViewHolder();
  62. convertView = infalInflater.inflate(R.layout.empty_layout,
  63. parent, false);
  64.  
  65. TextView tv = (TextView) convertView.findViewById(R.id.empty);
  66. emptyViewHolder.empty = tv;
  67.  
  68. //...initialize your viewcomponents of the populated layout here...
  69.  
  70. convertView.setTag(emptyViewHolder);
  71. break;
  72. case 1:
  73. populatedViewHolder = new PopulatedViewHolder();
  74. convertView = infalInflater.inflate(R.layout.list_item, null);
  75.  
  76. TextView tv1 = (TextView) convertView.findViewById(R.id.lblListItem);
  77. populatedViewHolder.UserName = tv1;
  78.  
  79. convertView.setTag(populatedViewHolder);
  80. }
  81. }
  82. else {
  83. switch (type) {
  84. case 0:
  85. emptyViewHolder = (EmptyViewHolder)convertView.getTag();
  86. break;
  87.  
  88. case 1:
  89. populatedViewHolder = (PopulatedViewHolder)convertView.getTag();
  90. break;
  91. }
  92. }
  93.  
  94. switch(type){
  95. case 0:
  96. //empty
  97. //set the values of your empty view here
  98. emptyViewHolder.empty.setText("test");
  99.  
  100. break;
  101. case 1:
  102. //populated
  103. //set the values of your populated view here
  104. populatedViewHolder.UserName.setText(childText);
  105.  
  106. break;
  107. }
  108.  
  109.  
  110. return convertView;
  111. }
  112.  
  113. @Override
  114. public int getChildrenCount(int groupPosition) {
  115. return this._listDataChild.get(this._listDataHeader.get(groupPosition))
  116. .size();
  117. }
  118.  
  119. @Override
  120. public Object getGroup(int groupPosition) {
  121. return this._listDataHeader.get(groupPosition);
  122. }
  123.  
  124. @Override
  125. public int getGroupCount() {
  126. return this._listDataHeader.size();
  127. }
  128.  
  129. @Override
  130. public long getGroupId(int groupPosition) {
  131. return groupPosition;
  132. }
  133.  
  134.  
  135. @Override
  136. public View getGroupView(int groupPosition, boolean isExpanded,
  137. View convertView, ViewGroup parent) {
  138. String headerTitle = (String) getGroup(groupPosition);
  139. if (convertView == null) {
  140. LayoutInflater infalInflater = (LayoutInflater) this._context
  141. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  142. convertView = infalInflater.inflate(R.layout.list_group, null);
  143. }
  144.  
  145. TextView lblListHeader = (TextView) convertView
  146. .findViewById(R.id.lblListHeader);
  147. lblListHeader.setTypeface(null, Typeface.BOLD);
  148. lblListHeader.setText(headerTitle);
  149.  
  150. return convertView;
  151. }
  152.  
  153. @Override
  154. public boolean hasStableIds() {
  155. return false;
  156. }
  157.  
  158. @Override
  159. public boolean isChildSelectable(int groupPosition, int childPosition) {
  160. return true;
  161. }
  162.  
  163. static class PopulatedViewHolder {
  164. //declare the elements of a populated view here
  165. public TextView UserName;
  166. }
  167.  
  168. static class EmptyViewHolder {
  169. //declare the elements of an empty view here
  170. public TextView empty;
  171. }
  172.  
  173. }