title: Android ToggleButton - setChecked in onCheckedChangeListener only works once
body: <p>So I have a dynamic list of items using an array list and an expandable list adapter, there is 1 child and 1 item per group. I have a favorite button and an alert button. Alerts are only possible if the item is also a favorite. To get started I am just trying to force the alert button's state onto the favorite button's state. I'll actually start checking the state and implementing the right behaviors once I can get this working.</p>
  
  <p>The problem is, the first time I check the alert button, the favorite button is also checked, after that setChecked in the listener seems to have no effect. Other child views in the list will also work the first time but then cease to work.</p>
  
  <pre><code>    private SparseArray&lt;ToggleButton&gt; favoriteButtons;
  
      ...
      @Override
  public View getChildView(int groupPosition, int childPosition,
          boolean isLastChild, View view, ViewGroup parent)
  {
      ...
      //Store the group position in the toggle button
      ToggleButton tapAlert = (ToggleButton)view.findViewById(R.id.tapAlertButton);
      tapAlert.setHint(Integer.valueOf(groupPosition).toString());
      //add favoriteButton to array at group position
      favoriteButtons.put(groupPosition, (ToggleButton)view.findViewById(R.id.favoriteButton));
  
      tapAlert.setOnCheckedChangeListener( new OnCheckedChangeListener()
      {
          @Override
          public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked)
          {
              int groupPosition = Integer.parseInt((String) toggleButton.getHint());
              ToggleButton curButton = favoriteButtons.get(groupPosition);
              curButton.setChecked(isChecked);
              //TODO
          }
      });
  </code></pre>
  
  <p>xml declarations</p>
  
  <pre><code>        &lt;ToggleButton 
              android:id="@+id/tapAlertButton"
              android:layout_width="50dp"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:textOn="@string/tap_alert"
              android:textOff="@string/tap_alert"
              android:textSize="12sp"
              android:lines="2"
          /&gt;
          &lt;ToggleButton
              android:id="@+id/favoriteButton"
              android:layout_width="50dp"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:textOn="@string/fav_list"
              android:textOff="@string/fav_list"
              android:textSize="12sp"
              android:lines="2"
              android:layout_marginBottom="6dp"
          /&gt;
  </code></pre>
  
  <p>Another question mentioned setting android:saveEnabled="true" or android:saveEnabled="false" but it doesn't seem to change the behavior.</p>
  
  <p><em>EDIT</em></p>
  
  <p>It seems that curButton no longer points to the child view's button on the second call to the listener.</p>
  
  <p><em>EDIT 2.5</em></p>
  
  <p>It seems getChildView can be pretty unpredictable, by only setting the listener the first time it is called, (the child is first expanded), everything seems to be working properly now. I'll post/accept and answer after the 8 hour time limit is up.</p>
  
  <pre><code>    if(favoriteButtons.get(groupPosition, null) == null)
      {
          //Store the group position in the toggle button
          ToggleButton tapAlert = (ToggleButton)view.findViewById(R.id.tapAlertButton);
          tapAlert.setHint(Integer.valueOf(groupPosition).toString());
          //add favoriteButton to array at group position
          favoriteButtons.put(groupPosition, (ToggleButton)view.findViewById(R.id.favoriteButton));
          tapAlert.setOnCheckedChangeListener( new OnCheckedChangeListener()
          {
              @Override
              public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked)
              {
                  int groupPosition = Integer.parseInt((String) toggleButton.getHint());
                  ToggleButton curButton = favoriteButtons.get(groupPosition);
                  curButton.setChecked(isChecked);
                  //TODO
              }
          });
      }
  </code></pre>