Frage

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.

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.

    private SparseArray<ToggleButton> 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
        }
    });

xml declarations

        <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"
        />
        <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"
        />

Another question mentioned setting android:saveEnabled="true" or android:saveEnabled="false" but it doesn't seem to change the behavior.

EDIT

It seems that curButton no longer points to the child view's button on the second call to the listener.

EDIT 2.5

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.

    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
            }
        });
    }
War es hilfreich?

Lösung

Fixed by only setting the listeners if they have not been set yet by checking my sparse array for null entries

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
        }
    });
}

Andere Tipps

You are setting it to "isChecked" instead of your own true/false.

So on the state change of a CheckButton, you more or less have the following.

If button IsChecked = false, then Button setChecked(false)

If button IsChecked = true, then Button setChecked(true)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top