Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么折叠ExpandableListView组会取消选中我的复选框?_Android_Android Listview - Fatal编程技术网

Android 为什么折叠ExpandableListView组会取消选中我的复选框?

Android 为什么折叠ExpandableListView组会取消选中我的复选框?,android,android-listview,Android,Android Listview,我有一个由BaseExpandableListAdapter支持的ExpandableListView。子列表项都是复选框 我展开一个组,选中复选框,然后折叠该组。然后取消选中该复选框 我的日志输出是: -- I check the checkbox: 02-27 13:51:29.674: D/ExpandableListAdapter(3583): Option '2' CHECKED --- I collapse the group, which unchecks the checkbox

我有一个由BaseExpandableListAdapter支持的ExpandableListView。子列表项都是复选框

我展开一个组,选中复选框,然后折叠该组。然后取消选中该复选框

我的日志输出是:

-- I check the checkbox:
02-27 13:51:29.674: D/ExpandableListAdapter(3583): Option '2' CHECKED
--- I collapse the group, which unchecks the checkbox:
02-27 13:51:30.850: D/ExpandableListAdapter(3583): Option '2' UNCHECKED
如您所见,折叠组就是(可能间接地)调用该组中所有复选框上的my OnCheckedChangedListener

我的代码是:

public class TestExpandableListActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ExpandableListView elv = 
            (ExpandableListView) findViewById(R.id.optionsELV);
        ExpandableListAdapter ela = new ExpandableListAdapter(this);
        elv.setAdapter(ela);
    }
    public class ExpandableListAdapter extends BaseExpandableListAdapter {
        private static final String TAG = "ExpandableListAdapter";
        private final String[] groups;
        private final String[][] children;
        private final boolean[][] childrenChecked;
        private final Context context;
        public ExpandableListAdapter(Context c) {
            this.context = c;
            groups = new String[] { "Options A", "Options B" };
            children = 
                new String[][] {{"A", "B", "C"},{"1", "2", "3"}};
            childrenChecked = 
                new boolean[][] {{false, false, false},{false, false, false}};
        }
        public View getChildView(
                final int groupPos, 
                final int childPos,
                boolean isLastChild, 
                View convertView, 
                ViewGroup parent) {
            CheckBox cb = new CheckBox(context);
            cb.setText(children[groupPos][childPos]);
            cb.setChecked(childrenChecked[groupPos][childPos]);
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(
                        CompoundButton buttonView, 
                        boolean isChecked) {
                    Log.d(TAG, 
                            String.format("Option '%s' %s", 
                                    children[groupPos][childPos], 
                                    isChecked ? "CHECKED" : "UNCHECKED"));
                    childrenChecked[groupPos][childPos] = isChecked;
                }
            });
            return cb;
        }
        public int getGroupCount() {
            return groups.length;
        }
        public int getChildrenCount(int groupPos) {
            return children[groupPos].length;
        }
        public String getGroup(int groupPos) {
            return groups[groupPos];
        }
        public String getChild(int groupPos, int childPos) {
            return children[groupPos][childPos];
        }
        public long getGroupId(int groupPos) {
            return (1024 * (groupPos + 1));
        }
        public long getChildId(int groupPos, int childPos) {
            return (1024 * (groupPos + 1)) + (childPos + 1);
        }
        public View getGroupView(
                int groupPos, 
                boolean isExpanded, 
                View convertView, 
                ViewGroup parent) {
            TextView tv = new TextView(context);
            tv.setText(groups[groupPos]);
            return tv;
        }
        public boolean hasStableIds() {
            return true;
        }
        public boolean isChildSelectable(int groupPos, int childPos) {
            return true;
        }
    }
}
我的布局(请注意,我已将choiceMode正确设置为MultipleEchoice)是:



为什么折叠组会取消选中我的所有复选框?如何防止这种情况发生?

我想我已经找到了解决方案-使用CheckedTextView而不是复选框

public View getChildView(
        final int groupPos, 
        final int childPos,
        boolean isLastChild, 
        View convertView, 
        ViewGroup parent) {
    final CheckedTextView ctv = new CheckedTextView(context);
    ctv.setChecked(childrenChecked[groupPos][childPos]);
    ctv.setCheckMarkDrawable(
            ctv.isChecked()
            ? android.R.drawable.checkbox_on_background 
            : android.R.drawable.checkbox_off_background);
    ctv.setText(children[groupPos][childPos]);
    ctv.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            ctv.toggle();
            Log.d(TAG, 
                String.format("Option '%s' %s", 
                    children[groupPos][childPos], 
                    ctv.isChecked() ? "CHECKED" : "UNCHECKED"));
            childrenChecked[groupPos][childPos] = ctv.isChecked();
            ctv.setCheckMarkDrawable(
                    ctv.isChecked() 
                    ? android.R.drawable.checkbox_on_background 
                    : android.R.drawable.checkbox_off_background);
        }
    });
    return ctv;
}
CheckedTextView文档确实说CheckedTextView在ListView中很有用-“当它的setChoiceMode被设置为CHOICE_MODE_NONE之外的值时,在ListView中使用它很有用”-但我不清楚CheckedTextView和CheckedTextView之间的区别,以及为什么复选框似乎完全不起作用


有谁能告诉我CheckBox和CheckedTextView之间的区别,以及为什么CheckBox在ExpandableListView中的功能如此奇怪?

我想我找到了解决方案-使用CheckedTextView而不是CheckBox

public View getChildView(
        final int groupPos, 
        final int childPos,
        boolean isLastChild, 
        View convertView, 
        ViewGroup parent) {
    final CheckedTextView ctv = new CheckedTextView(context);
    ctv.setChecked(childrenChecked[groupPos][childPos]);
    ctv.setCheckMarkDrawable(
            ctv.isChecked()
            ? android.R.drawable.checkbox_on_background 
            : android.R.drawable.checkbox_off_background);
    ctv.setText(children[groupPos][childPos]);
    ctv.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            ctv.toggle();
            Log.d(TAG, 
                String.format("Option '%s' %s", 
                    children[groupPos][childPos], 
                    ctv.isChecked() ? "CHECKED" : "UNCHECKED"));
            childrenChecked[groupPos][childPos] = ctv.isChecked();
            ctv.setCheckMarkDrawable(
                    ctv.isChecked() 
                    ? android.R.drawable.checkbox_on_background 
                    : android.R.drawable.checkbox_off_background);
        }
    });
    return ctv;
}
CheckedTextView文档确实说CheckedTextView在ListView中很有用-“当它的setChoiceMode被设置为CHOICE_MODE_NONE之外的值时,在ListView中使用它很有用”-但我不清楚CheckedTextView和CheckedTextView之间的区别,以及为什么复选框似乎完全不起作用


有谁能告诉我复选框和CheckedTextView之间的区别,以及为什么复选框在ExpandableListView中的功能如此奇怪?

我也有同样的问题。我无法告诉您原因,但是移动、扩展和折叠ExpandableListView的段有时会导致复选框取消选中

解决这一问题的一种更简单的方法是使用OnClickListener事件for复选框,而不是OnCheckedChange


真烦人

我也有同样的问题。我无法告诉您原因,但是移动、扩展和折叠ExpandableListView的段有时会导致复选框取消选中

解决这一问题的一种更简单的方法是使用OnClickListener事件for复选框,而不是OnCheckedChange


即使在选中的文本视图中,如果您折叠或展开文本视图,随机切换列表其余部分的更改,这也很烦人,您是否遇到过此类问题?即使在选中的文本视图中,如果您折叠或展开文本视图,随机切换列表其余部分的更改,您是否遇到过此类问题?