ExpandableListView的ChildView中的CheckBox

问题描述 投票:1回答:1

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

我正在从数据库中检索checkbox的值并将其存储在整数ArrayList中,值为0和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;
 }
}

但我正在获取复选框的未选中值。此外,在选中复选框并折叠和展开组时,我再次取消选中复选框。

android checkbox expandablelistview
1个回答
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

            }

        }
    }


}
© www.soinside.com 2019 - 2024. All rights reserved.