Guest User

ExpandListAdapter

a guest
Oct 9th, 2012
187
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.sentaca.android.accordion.adapter;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.view.ViewGroup;
  12. import android.widget.BaseExpandableListAdapter;
  13. import android.widget.Button;
  14. import android.widget.TextView;
  15.  
  16. import com.sentaca.android.accordion.R;
  17. import com.sentaca.android.accordion.classes.ExpandListChild;
  18. import com.sentaca.android.accordion.classes.ExpandListGroup;
  19.  
  20. public abstract class ExpandListAdapter extends BaseExpandableListAdapter {
  21.     private Context context;
  22.     private ArrayList<ExpandListGroup> groups;
  23.     private Activity parent;
  24.    
  25.     /**
  26.      * Abstract function that will be implemented in the sub classes
  27.      */
  28.     public abstract View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent);
  29.    
  30.     public ExpandListAdapter(Context context, ArrayList<ExpandListGroup> groups) {
  31.         this.context = context;
  32.         this.groups = groups;
  33.         parent = (Activity) context;
  34.     }
  35.    
  36.     public void addItem(ExpandListChild item, ExpandListGroup group) {
  37.         //If it doesn't exist. Add it to the list.
  38.         if (!groups.contains(group)) {
  39.             groups.add(group);
  40.             group.setItems(new ArrayList<ExpandListChild>());
  41.         }
  42.        
  43.         //Then grab the index of the group that you just added.
  44.         int index = groups.indexOf(group);
  45.        
  46.         //Update the groups list of items
  47.         ArrayList<ExpandListChild> ch = groups.get(index).getItems();
  48.         ch.add(item);
  49.         groups.get(index).setItems(ch);
  50.     }
  51.    
  52.     public ExpandListChild getChild(int groupPosition, int childPosition) {
  53.         ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems();
  54.         return chList.get(childPosition);
  55.     }
  56.  
  57.     public long getChildId(int groupPosition, int childPosition) {
  58.         return childPosition;
  59.     }
  60.  
  61.     public int getChildrenCount(int groupPosition) {
  62.         ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems();
  63.  
  64.         return chList.size();
  65.     }
  66.  
  67.     public Object getGroup(int groupPosition) {
  68.         return groups.get(groupPosition);
  69.     }
  70.  
  71.     public int getGroupCount() {
  72.         return groups.size();
  73.     }
  74.  
  75.     public long getGroupId(int groupPosition) {
  76.         return groupPosition;
  77.     }
  78.  
  79.     public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) {
  80.         ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);
  81.        
  82.         if (view == null) {
  83.             LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
  84.             view = inf.inflate(R.layout.expandlist_group_item, null);
  85.         }
  86.        
  87.         Log.d("MyDebugStatements", group.getName());
  88.  
  89.         //Change the groups name to the appropriate string to be displayed
  90.         TextView tv = (TextView) view.findViewById(R.id.tvGroup);
  91.         tv.setText(group.getName());
  92.         return view;
  93.     }
  94.  
  95.     public boolean hasStableIds() {
  96.         return true;
  97.     }
  98.  
  99.     public boolean isChildSelectable(int arg0, int arg1) {
  100.         return true;
  101.     }
  102. }
RAW Paste Data