ExpandableListAdapter.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package de.tudarmstadt.informatik.hostage.ui2.adapter;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseExpandableListAdapter;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import de.tudarmstadt.informatik.hostage.ui2.model.ExpandableListItem;
  11. /**
  12. * Created by Julien on 06.02.14.
  13. */
  14. public abstract class ExpandableListAdapter extends BaseExpandableListAdapter {
  15. private Context _context;
  16. // header titles
  17. public List<String> _sectionHeader;
  18. // data in format of header title, childs list
  19. public HashMap<String, ArrayList<ExpandableListItem>> _sectionTitleToChildData;
  20. /**
  21. * Constructor
  22. * @param context the context
  23. * @param listSectionHeaders the section title
  24. * @param dataMapping {@link ExpandableListItem ExpandableListItem} the data to visualise
  25. */
  26. public ExpandableListAdapter(Context context, List<String> listSectionHeaders,
  27. HashMap<String, ArrayList<ExpandableListItem>> dataMapping) {
  28. this._context = context;
  29. this._sectionHeader = listSectionHeaders;
  30. this._sectionTitleToChildData = dataMapping;
  31. }
  32. public void setData(HashMap<String, ArrayList<ExpandableListItem>> dataMapping){
  33. this._sectionTitleToChildData = dataMapping;
  34. }
  35. public HashMap<String, ArrayList<ExpandableListItem>> getData(){
  36. return this._sectionTitleToChildData;
  37. }
  38. public void setSectionHeader(List<String> listSectionHeaders){
  39. this._sectionHeader = listSectionHeaders;
  40. }
  41. public List<String> getSectionHeaders(){
  42. return this._sectionHeader;
  43. }
  44. @Override
  45. public Object getChild(int section, int row) {
  46. return this._sectionTitleToChildData.get(this._sectionHeader.get(section))
  47. .get(row);
  48. }
  49. @Override
  50. public long getChildId(int section, int row) {
  51. return row;
  52. }
  53. @Override
  54. public View getChildView(int section, final int row,
  55. boolean isLastChild, View convertView, ViewGroup parent) {
  56. if (convertView == null) {
  57. LayoutInflater infalInflater = (LayoutInflater) this._context
  58. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  59. convertView = infalInflater.inflate(this.getCellLayoutID(), null);
  60. }
  61. this.configureCellView(convertView, section, row);
  62. return convertView;
  63. }
  64. @Override
  65. public int getChildrenCount(int section) {
  66. return this._sectionTitleToChildData.get(this._sectionHeader.get(section))
  67. .size();
  68. }
  69. @Override
  70. public Object getGroup(int section) {
  71. return this._sectionHeader.get(section);
  72. }
  73. @Override
  74. public int getGroupCount() {
  75. return this._sectionHeader.size();
  76. }
  77. @Override
  78. public long getGroupId(int section) {
  79. return section;
  80. }
  81. @Override
  82. public View getGroupView(int section, boolean isExpanded,
  83. View convertView, ViewGroup parent) {
  84. if (convertView == null) {
  85. LayoutInflater infalInflater = (LayoutInflater) this._context
  86. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  87. convertView = infalInflater.inflate(this.getSectionLayoutID(), null);
  88. }
  89. this.configureSectionHeaderView(convertView, section);
  90. return convertView;
  91. }
  92. /**
  93. * Return the {@link ExpandableListItem ExpandableListItem} for the given index path
  94. * @param section int
  95. * @param row int
  96. * @return {@link ExpandableListItem ExpandableListItem}
  97. */
  98. public ExpandableListItem getDataForRow(int section, int row){
  99. return this._sectionTitleToChildData.get(this._sectionHeader.get(section)).get(row);
  100. }
  101. /**
  102. * Configure the items root view in here
  103. * @param cell View, the root view
  104. * @param section int
  105. * @param row int
  106. */
  107. public abstract void configureCellView(View cell, int section, int row);
  108. public abstract void configureSectionHeaderView(View sectionHeader, int section);
  109. /**
  110. * Returns the section header layout id.
  111. * @return R.layout.list_section
  112. * */
  113. public abstract int getSectionLayoutID();
  114. /**
  115. * Return the root view layout id.
  116. * @return R.layout.list_cell
  117. * */
  118. public abstract int getCellLayoutID();
  119. @Override
  120. public boolean hasStableIds() {
  121. return false;
  122. }
  123. @Override
  124. public boolean isChildSelectable(int section, int row) {
  125. return true;
  126. }
  127. }