Guest User

Untitled

a guest
Jan 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. public class MyCustomAdapter extends BaseExpandableListAdapter {
  2.  
  3. private LayoutInflater inflater;
  4. private ArrayList<Parent> mParent;
  5. private Context Kontext;
  6. private int count = 0;
  7. Children child;
  8. private CheckBox childCheckBox;
  9.  
  10.  
  11. public MyCustomAdapter(Context context, ArrayList<Parent> parent) {
  12. mParent = parent;
  13. inflater = LayoutInflater.from(context);
  14. Kontext = context;
  15. }
  16.  
  17. @Override
  18. //counts the number of group/parent items so the list knows how many times calls getGroupView() method
  19. public int getGroupCount() {
  20. return mParent.size();
  21. }
  22.  
  23. @Override
  24. //counts the number of children items so the list knows how many times calls getChildView() method
  25. public int getChildrenCount(int groupPosition) {
  26. Log.e("EXPANDABLE LIST", "GROUP " + groupPosition + " " + mParent.get(groupPosition).getArrayChildren()
  27. .size() + "");
  28. return mParent.get(groupPosition).getArrayChildren().size();
  29. }
  30.  
  31. @Override
  32. //gets the title of each parent/group
  33. public Object getGroup(int i) {
  34. return mParent.get(i).getTitle();
  35. }
  36.  
  37. @Override
  38. //gets the name of each item
  39. public Object getChild(int i, int i1) {
  40. return mParent.get(i).getArrayChildren().get(i1);
  41. }
  42.  
  43. @Override
  44. public long getGroupId(int i) {
  45. return i;
  46. }
  47.  
  48. @Override
  49. public long getChildId(int i, int i1) {
  50. return i1;
  51. }
  52.  
  53. @Override
  54. public boolean hasStableIds() {
  55. return true;
  56. }
  57.  
  58. @Override
  59. public int getChildTypeCount() {
  60. return 6;
  61. }
  62.  
  63. @Override
  64. //in this method you must set the text to see the parent/group on the list
  65. public View getGroupView(int groupPosition, boolean b, View convertView, ViewGroup viewGroup) {
  66.  
  67. ViewHolder holder = new ViewHolder();
  68. holder.groupPosition = groupPosition;
  69.  
  70. if (convertView == null) {
  71. convertView = inflater.inflate(R.layout.list_item_parent, viewGroup, false);
  72. holder.groupTitle = (TextView) convertView.findViewById(R.id.list_item_text_view);
  73.  
  74. convertView.setTag(holder);
  75. } else {
  76. holder = (ViewHolder) convertView.getTag();
  77. }
  78.  
  79. holder.groupTitle.setText(getGroup(groupPosition).toString());
  80.  
  81. //return the entire view
  82. return convertView;
  83. }
  84.  
  85. @Override
  86. //in this method you must set the text to see the children on the list
  87. public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
  88.  
  89. final ViewHolderChild holder;
  90. child = mParent.get(groupPosition).getArrayChildren().get
  91. (childPosition);
  92.  
  93. if (convertView == null) {
  94. holder = new ViewHolderChild();
  95. convertView = inflater.inflate(R.layout.child_list_layout, viewGroup, false);
  96. holder.childTitle = (TextView) convertView.findViewById(R.id.list_item_text_child);
  97. holder.childCheckBox = (CheckBox) convertView.findViewById((R.id.cbListItemChild));
  98.  
  99. convertView.setTag(holder);
  100. } else {
  101. holder = (ViewHolderChild) convertView.getTag();
  102. }
  103.  
  104. holder.childTitle.setText(mParent.get(groupPosition).getArrayChildren().get(childPosition).getArtikelName());
  105.  
  106. convertView.setOnClickListener(new View.OnClickListener() {
  107. @Override
  108. public void onClick(View v) {
  109. Toast.makeText(Kontext.getApplicationContext(), "gekauft : " +mParent.get(groupPosition).getArrayChildren().get
  110. (childPosition).gekauft
  111. + "", Toast.LENGTH_SHORT).show();
  112.  
  113.  
  114. }
  115. });
  116.  
  117. holder.childCheckBox.setOnClickListener(new View.OnClickListener() {
  118. @Override
  119. public void onClick(View v) {
  120. Toast.makeText(Kontext.getApplicationContext(), "You have selected item : " +mParent.get(groupPosition).getArrayChildren().get
  121. (childPosition).name
  122. + "", Toast.LENGTH_SHORT).show();
  123.  
  124. addChildToGroup(mParent.get(groupPosition).getArrayChildren().get
  125. (childPosition), mParent.size() - 1, "Parent 25 (Hardcoded)");
  126. removeChildFromGroup(groupPosition, childPosition);
  127. notifyDataSetChanged();
  128.  
  129. }
  130. });
  131.  
  132.  
  133. //return the entire view
  134. return convertView;
  135. }
  136.  
  137. @Override
  138. public boolean isChildSelectable(int i, int i1) {
  139. return true;
  140. }
  141.  
  142. public void removeGroup(int groupPosition) {
  143. mParent.remove(groupPosition);
  144. Log.e("removeGroup", "group size " + mParent.size());
  145. notifyDataSetChanged();
  146. }
  147.  
  148. /**
  149. * Removes child at childPosition from group at groupPosition. If it is the last child in group
  150. * the group will also be deleted.
  151. *
  152. * @param groupPosition Position of the group in which the child exists
  153. * @param childPosition Position of the child in the group
  154. */
  155. public void removeChildFromGroup(int groupPosition, int childPosition) {
  156. Parent parent = mParent.get(groupPosition);
  157.  
  158. parent.getArrayChildren().remove(childPosition);
  159.  
  160. if (parent.getArrayChildren().isEmpty()) {
  161. removeGroup(groupPosition);
  162. } else {
  163. notifyDataSetChanged();
  164. }
  165. }
  166.  
  167. public void addChildToGroup(Children childName, int groupPosition, String sectionTitle) {
  168. Parent parent = mParent.get(groupPosition);
  169. List<Children> arrayChildren = parent.getArrayChildren();
  170.  
  171.  
  172. if (!parent.getTitle().equals(sectionTitle)) {
  173. parent = new Parent();
  174. arrayChildren = new ArrayList<>();
  175.  
  176. parent.setTitle(sectionTitle);
  177.  
  178. mParent.add(parent);
  179. }
  180.  
  181. arrayChildren.add(childName);
  182. parent.setArrayChildren(arrayChildren);
  183. notifyDataSetChanged();
  184. }
  185.  
  186.  
  187. protected class ViewHolder {
  188. protected int groupPosition;
  189. protected TextView groupTitle;
  190. protected TextView childTitle;
  191. }
  192.  
  193. private class ViewHolderChild {
  194.  
  195. TextView childTitle;
  196. CheckBox childCheckBox;
  197. View viewDivider;
  198. }}
Add Comment
Please, Sign In to add comment