ExpandableListAdapter.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package de.tudarmstadt.informatik.hostage.ui2.adapter;
  2. import android.view.View;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import android.content.Context;
  7. import android.view.LayoutInflater;
  8. import android.view.ViewGroup;
  9. import android.widget.BaseExpandableListAdapter;
  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. @Override
  28. public Object getChild(int section, int row) {
  29. return this._sectionTitleToChildData.get(this._sectionHeader.get(section))
  30. .get(row);
  31. }
  32. @Override
  33. public long getChildId(int section, int row) {
  34. return row;
  35. }
  36. @Override
  37. public View getChildView(int section, final int row,
  38. boolean isLastChild, View convertView, ViewGroup parent) {
  39. if (convertView == null) {
  40. LayoutInflater infalInflater = (LayoutInflater) this._context
  41. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  42. convertView = infalInflater.inflate(this.getCellLayoutID(), null);
  43. }
  44. this.configureCellView(convertView, section, row);
  45. return convertView;
  46. }
  47. @Override
  48. public int getChildrenCount(int section) {
  49. return this._sectionTitleToChildData.get(this._sectionHeader.get(section))
  50. .size();
  51. }
  52. @Override
  53. public Object getGroup(int section) {
  54. return this._sectionHeader.get(section);
  55. }
  56. @Override
  57. public int getGroupCount() {
  58. return this._sectionHeader.size();
  59. }
  60. @Override
  61. public long getGroupId(int section) {
  62. return section;
  63. }
  64. @Override
  65. public View getGroupView(int section, boolean isExpanded,
  66. View convertView, ViewGroup parent) {
  67. if (convertView == null) {
  68. LayoutInflater infalInflater = (LayoutInflater) this._context
  69. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  70. convertView = infalInflater.inflate(this.getSectionLayoutID(), null);
  71. }
  72. this.configureSectionHeaderView(convertView, section);
  73. return convertView;
  74. }
  75. public ExpandableListItem getDataForRow(int section, int row){
  76. return this._sectionTitleToChildData.get(this._sectionHeader.get(section)).get(row);
  77. }
  78. public abstract void configureCellView(View cell, int section, int row);
  79. public abstract void configureSectionHeaderView(View sectionHeader, int section);
  80. /*
  81. * @return R.layout.list_section
  82. * */
  83. public abstract int getSectionLayoutID();
  84. /*
  85. * @return R.layout.list_cell
  86. * */
  87. public abstract int getCellLayoutID();
  88. @Override
  89. public boolean hasStableIds() {
  90. return false;
  91. }
  92. @Override
  93. public boolean isChildSelectable(int section, int row) {
  94. return true;
  95. }
  96. }