title: ExpandableListView - Expand and collapse Groups
body: <p>Im trying to expand and collapse groups if the group id is in a arraylist I have.</p>
  
  <p>This is how i do it:</p>
  
  <pre><code>final Group group = (Group) getGroup(groupPosition);   
  ExpandableListView eLV = (ExpandableListView) parent;
  if ( subscriptions != null ) {
      if ( subscriptions.contains(group.getID()) ) {
          ((CheckBox) v.findViewById(R.id.check_channel)).setChecked(true);
          eLV.expandGroup(groupPosition);
      } else {
          ((CheckBox) v.findViewById(R.id.check_channel)).setChecked(false);
          eLV.collapseGroup(groupPosition);
      }
  }
  </code></pre>
  
  <p>It works the first time when the groups are loaded and the correct ones are expanded. also when I press on the group, it collapses as expected.</p>
  
  <p>BUT, when I want to expand a group, or select a group, the app crashes. ITs only when I remove the part <strong>eLV.collapseGroup(groupPosition);</strong> from the else block that it works again. but then I cant collapse then for obvious reasons..</p>
  
  <p>This is the exception:</p>
  
  <pre><code>03-27 09:29:11.059: W/dalvikvm(21674): threadid=1: thread exiting with uncaught exception (group=0x40bec1f8)
  03-27 09:29:11.067: E/AndroidRuntime(21674): FATAL EXCEPTION: main
  03-27 09:29:11.067: E/AndroidRuntime(21674): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at java.util.ArrayList.get(ArrayList.java:304)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:225)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.AbsListView.obtainView(AbsListView.java:2210)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.ListView.onMeasure(ListView.java:1155)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1027)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:565)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2240)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1092)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.os.Handler.dispatchMessage(Handler.java:99)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.os.Looper.loop(Looper.java:137)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.app.ActivityThread.main(ActivityThread.java:4514)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
  03-27 09:29:11.067: E/AndroidRuntime(21674):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>The complete function:</p>
  
  <pre><code>public View getGroupView(int groupPosition, boolean isExpanded, View convertView,ViewGroup parent) { 
      View v;
      if (convertView == null) {
          v = newGroupView(isExpanded, parent);
      } else {
          v = convertView;
      }
  
  
  
      final Group group = (Group) getGroup(groupPosition);   
      ExpandableListView eLV = (ExpandableListView) parent;
      if ( subscriptions != null ) {
          if ( subscriptions.contains(group.getID()) ) {
              ((CheckBox) v.findViewById(R.id.check_channel)).setChecked(true);
              eLV.expandGroup(groupPosition);
          } else {
              ((CheckBox) v.findViewById(R.id.check_channel)).setChecked(false);
              eLV.collapseGroup(groupPosition);
          }
      }
  
      if ( isExpanded ) {
          if ( subscriptions.contains(group.getID()) == false ) {
              subscriptions.add(group.getID());
          }
      } else {
          for (int i=0;i&lt;subscriptions.size();i++) {
              if ( subscriptions.get(i).equals(group.getID())) {
                  subscriptions.remove(i);
              }
          }
      }
  
      bindGroupView(groupPosition, isExpanded, v, parent);
      return v;
  }
  </code></pre>