HoloEverywhere: BaseExpandableListAdapter getChildView и состояние флажка

Я использую библиотеку Holoeverywhere и соответствующие классы: ExpandableListview, BaseExpandableListAdapter.

getChildView класса, расширяющего BaseExpandableListAdapter, содержит:

    checkBox.setText(option.getTitle());
    checkBox.setChecked(true);

Когда элемент checkBox визуализируется, текст устанавливается правильно, но его состояние остается неотмеченным. Почему не проверяется?

Пример адаптера:

/**
 * A simple adapter which maintains an ArrayList of photo resource Ids.
 * Each photo is displayed as an image. This adapter supports clearing the
 * list of photos and adding a new photo.
 *
 */
public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
    private String[][] children = {
            { "Arnold", "Barry", "Chuck", "David" },
            { "Ace", "Bandit", "Cha-Cha", "Deuce" },
            { "Fluffy", "Snuggles" },
            { "Goldy", "Bubbles" }
    };

    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    public android.widget.TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        TextView textView = new TextView(TestActivity.this);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setPadding(36, 0, 0, 0);
        return textView;
    }

    public CheckBox getCheckbox() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        CheckBox checkBox = new CheckBox(TestActivity.this);
        checkBox.setLayoutParams(lp);
        // Center the text vertically
        checkBox.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        checkBox.setPadding(36, 0, 0, 0);
        return checkBox;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {
        CheckBox checkBox = (CheckBox) convertView;
        if(checkBox == null) {
            checkBox = getCheckbox();
        }

        checkBox.setChecked(true);
        checkBox.setText(getChild(groupPosition, childPosition).toString());
        return checkBox;
    }

    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    public int getGroupCount() {
        return groups.length;
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                             ViewGroup parent) {
        android.widget.TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }

}

биты макета:

<org.holoeverywhere.widget.ExpandableListView
        android:id="@+id/expandable_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="multipleChoice"/>

person midnight    schedule 02.08.2013    source источник


Ответы (3)


Я точно не знаю, в чем проблема с вашим кодом, но предлагаю вам попробовать изменить CheckBox на CheckedTextView http://developer.android.com/reference/android/widget/CheckedTextView.html

person Tomer Mor    schedule 05.08.2013
comment
тот же результат, к сожалению - person midnight; 05.08.2013

Возможно, вам придется попытаться объявить CheckBox публично.

person Pratik Butani    schedule 05.08.2013

Это только возможность, но вместо этого попробуйте checkBox.toggle()

person Shadesblade    schedule 12.08.2013