ExpandableListView的ChildView中的CheckBox

时间:2015-01-01 05:19:52

标签: android checkbox expandablelistview

在我的Android应用程序中,我有一个ExpandableListView,每个组只有一个子节点,其中包含一个复选框。我在设置复选框的值时遇到了麻烦。

我从数据库中检索checkbox的值并将其存储在值为0&的整数ArrayList中。 1分别为已选中和未选中

ArrayList<Integer> trustedList = new ArrayList<Integer>();

最初,我已将数据库中的所有条目都设为1。

ExpandableListView的Adapter类如下:

class DataAdapter extends BaseExpandableListAdapter {

    private Context context;
    LayoutInflater lat = getLayoutInflater();

    CheckBox cb;

    public DataAdapter(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

@Override
    public View getChildView(int group, int child, boolean arg2, View v,
            ViewGroup arg4) {
        // TODO Auto-generated method stub

        v = lat.inflate(R.layout.inflate_list, null);
        cb = (CheckBox) v.findViewById(R.id.checkBox1);

        if (trustedList.get(group) == 1)
            cb.setChecked(true);
        else
            cb.setChecked(false);

    return v;
 }
}

但是我为复选框获取了未经检查的值。此外,在选中复选框并折叠和展开论坛后,我再次取消选中此复选框。

1 个答案:

答案 0 :(得分:0)

class DataAdapter extends BaseExpandableListAdapter {

    private Context context;
    LayoutInflater lat = getLayoutInflater();

    CheckBox cb;

    public DataAdapter(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

@Override
    public View getChildView(int group, int child, boolean arg2, View v,
            ViewGroup arg4) {
        // TODO Auto-generated method stub

        v = lat.inflate(R.layout.inflate_list, null);
        cb = (CheckBox) v.findViewById(R.id.checkBox1);

        if (trustedList.get(group) == 1)
            cb.setChecked(true);
        else
            cb.setChecked(false);
cb.setOnCheckChangeListener(new CheckchangeListener(group) );

    return v;
 }
class CheckchangeListener implements OnCheckedChangeListener {
        private int position;

        public CheckchangeListener(int position) {
            // TODO Auto-generated constructor stub

            this.position= position;

        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            if (isChecked) {
                //updateyour list and database here

            } else {
                //updateyour list and database here

            }

        }
    }


}