ExpandableListAdapter.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /*CONSTRUCTOR*/
  21. public ExpandableListAdapter(Context context, List<String> listSectionHeaders,
  22. HashMap<String, ArrayList<ExpandableListItem>> dataMapping) {
  23. this._context = context;
  24. this._sectionHeader = listSectionHeaders;
  25. this._sectionTitleToChildData = dataMapping;
  26. }
  27. public void setData(HashMap<String, ArrayList<ExpandableListItem>> dataMapping){
  28. this._sectionTitleToChildData = dataMapping;
  29. }
  30. public HashMap<String, ArrayList<ExpandableListItem>> getData(){
  31. return this._sectionTitleToChildData;
  32. }
  33. public void setSectionHeader(List<String> listSectionHeaders){
  34. this._sectionHeader = listSectionHeaders;
  35. }
  36. public List<String> getSectionHeaders(){
  37. return this._sectionHeader;
  38. }
  39. @Override
  40. public Object getChild(int section, int row) {
  41. return this._sectionTitleToChildData.get(this._sectionHeader.get(section))
  42. .get(row);
  43. }
  44. @Override
  45. public long getChildId(int section, int row) {
  46. return row;
  47. }
  48. @Override
  49. public View getChildView(int section, final int row,
  50. boolean isLastChild, View convertView, ViewGroup parent) {
  51. if (convertView == null) {
  52. LayoutInflater infalInflater = (LayoutInflater) this._context
  53. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  54. convertView = infalInflater.inflate(this.getCellLayoutID(), null);
  55. }
  56. this.configureCellView(convertView, section, row);
  57. return convertView;
  58. }
  59. @Override
  60. public int getChildrenCount(int section) {
  61. return this._sectionTitleToChildData.get(this._sectionHeader.get(section))
  62. .size();
  63. }
  64. @Override
  65. public Object getGroup(int section) {
  66. return this._sectionHeader.get(section);
  67. }
  68. @Override
  69. public int getGroupCount() {
  70. return this._sectionHeader.size();
  71. }
  72. @Override
  73. public long getGroupId(int section) {
  74. return section;
  75. }
  76. @Override
  77. public View getGroupView(int section, boolean isExpanded,
  78. View convertView, ViewGroup parent) {
  79. if (convertView == null) {
  80. LayoutInflater infalInflater = (LayoutInflater) this._context
  81. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  82. convertView = infalInflater.inflate(this.getSectionLayoutID(), null);
  83. }
  84. this.configureSectionHeaderView(convertView, section);
  85. return convertView;
  86. }
  87. public ExpandableListItem getDataForRow(int section, int row){
  88. return this._sectionTitleToChildData.get(this._sectionHeader.get(section)).get(row);
  89. }
  90. public abstract void configureCellView(View cell, int section, int row);
  91. public abstract void configureSectionHeaderView(View sectionHeader, int section);
  92. /*
  93. * @return R.layout.list_section
  94. * */
  95. public abstract int getSectionLayoutID();
  96. /*
  97. * @return R.layout.list_cell
  98. * */
  99. public abstract int getCellLayoutID();
  100. @Override
  101. public boolean hasStableIds() {
  102. return false;
  103. }
  104. @Override
  105. public boolean isChildSelectable(int section, int row) {
  106. return true;
  107. }
  108. }