Advertisement
Guest User

lab2 - adapter

a guest
Nov 20th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. package com.example.lab2;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Color;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseExpandableListAdapter;
  9. import android.widget.ExpandableListAdapter;
  10. import android.widget.TextView;
  11.  
  12. import java.util.List;
  13. import java.util.Map;
  14.  
  15. public class MyExpandableListAdapter extends BaseExpandableListAdapter {
  16.  
  17.     Context context;
  18.     List<String> parents;
  19.     Map<String, List<String>> children;
  20.  
  21.     public MyExpandableListAdapter(Context context, List<String> parents, Map<String, List<String>> children) {
  22.         this.context = context;
  23.         this.parents = parents;
  24.         this.children = children;
  25.     }
  26.  
  27.  
  28.     @Override
  29.     public int getGroupCount() {
  30.         return parents.size();
  31.     }
  32.  
  33.     @Override
  34.     public int getChildrenCount(int groupPosition) {
  35.         return children.get(parents.get(groupPosition)).size();
  36.     }
  37.  
  38.     @Override
  39.     public Object getGroup(int groupPosition) {
  40.         return parents.get(groupPosition);
  41.     }
  42.  
  43.     @Override
  44.     public Object getChild(int groupPosition, int childPosition) {
  45.         return children.get(parents.get(groupPosition)).get(childPosition);
  46.     }
  47.  
  48.     @Override
  49.     public long getGroupId(int groupPosition) {
  50.         return groupPosition;
  51.     }
  52.  
  53.     @Override
  54.     public long getChildId(int groupPosition, int childPosition) {
  55.         return childPosition;
  56.     }
  57.  
  58.     @Override
  59.     public boolean hasStableIds() {
  60.         return false;
  61.     }
  62.  
  63.     @Override
  64.     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
  65.  
  66.         String title = (String) getGroup(groupPosition);
  67.  
  68.         if(convertView == null){
  69.             LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
  70.             convertView = inflater.inflate(R.layout.parent_list, null);
  71.         }
  72.  
  73.         TextView txtParent = (TextView) convertView.findViewById(R.id.textParent);
  74.         txtParent.setText(title);
  75.  
  76.         return convertView;
  77.     }
  78.  
  79.     @Override
  80.     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  81.  
  82.         String child = (String) getChild(groupPosition, childPosition);
  83.  
  84.         if(convertView == null){
  85.             LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
  86.             convertView = inflater.inflate(R.layout.child_list, null);
  87.         }
  88.  
  89.         TextView txtChild = (TextView) convertView.findViewById(R.id.textChild);
  90.         txtChild.setText(child);
  91.  
  92.         return convertView;
  93.     }
  94.  
  95.     @Override
  96.     public boolean isChildSelectable(int groupPosition, int childPosition) {
  97.         return true;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement