接线起来expandablelistview级联复选框 [英] wiring up expandablelistview cascading checkboxes

查看:255
本文介绍了接线起来expandablelistview级联复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很好的学习经验,但我最后几乎与我expandablelistview适配器完成。这对组复选框和childviews等等,直观,您可以禁用/启用了该小组认为复选框行动childviews所有的检查。所有现在剩下的就是让组视图复选框对应孩子的意见。当我尝试它崩溃。

 的for(int i = 0; I< check_states.get(groupPosition).size();我++){
        如果(check_states.get(groupPosition)获得(一)== FALSE){
            Log.d(肉,子复选框是不是都检查!!!);
        }
    }

我现在非常肯定,这是因为我所说的ArrayList是完成填充之前,这些命令 - >check_states是,我用它来保持复选框的checkstates的ArrayLists的ArrayList。

所以我的问题是很干脆如何等待数组列表完成之前填充我打电话给我的命令。我看着我所能,但他们似乎有点什么比这更值得参与。我宁愿设置不是建立一个单独的线程或备用线10 code的东西这么微薄的,因为这很长的点击菜单。

编辑:用logcat的更新

  10-06 19:18:08.204:W / dalvikvm(540):主题ID = 1:螺纹未捕获的异常退出(组= 0x40015560)
10-06 19:18:08.224:E / AndroidRuntime(540):致命异常:主要
10-06 19:18:08.224:E / AndroidRuntime(540):java.lang.IndexOutOfBoundsException:无效指数为0,大小为0
10-06 19:18:08.224:E / AndroidRuntime(540):在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
10-06 19:18:08.224:E / AndroidRuntime(540):在java.util.ArrayList.get(ArrayList.java:311)
10-06 19:18:08.224:E / AndroidRuntime(540):在com.mangodeveloper.mcathomie.McatTopicsExpandableListAdapter.getGroupView(McatTopicsExpandableListAdapter.java:166)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:445)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.AbsListView.obtainView(AbsListView.java:1430)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.ListView.makeAndAddView(ListView.java:1745)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.ListView.fillDown(ListView.java:670)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.ListView.fillFromTop(ListView.java:727)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.ListView.layoutChildren(ListView.java:1598)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.AbsListView.onLayout(AbsListView.java:1260)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.view.View.layout(View.java:7175)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.view.View.layout(View.java:7175)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.view.View.layout(View.java:7175)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.view.View.layout(View.java:7175)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.view.View.layout(View.java:7175)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.view.View.layout(View.java:7175)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.os.Handler.dispatchMessage(Handler.java:99)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.os.Looper.loop(Looper.java:123)
10-06 19:18:08.224:E / AndroidRuntime(540):在android.app.ActivityThread.main(ActivityThread.java:3683)
10-06 19:18:08.224:E / AndroidRuntime(540):在java.lang.reflect.Method.invokeNative(本机方法)
10-06 19:18:08.224:E / AndroidRuntime(540):在java.lang.reflect.Method.invoke(Method.java:507)
10-06 19:18:08.224:E / AndroidRuntime(540):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
10-06 19:18:08.224:E / AndroidRuntime(540):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-06 19:18:08.224:E / AndroidRuntime(540):在dalvik.system.NativeStart.main(本机方法)

再次编辑:这里只是对扩展适配器的相关部分。

 公共类McatTopicsExpandableListAdapter扩展BaseExpandableListAdapter {...公共查看getChildView(最终诠释groupPosition,最终诠释childPosition,布尔isLastChild,
            查看convertView,父母的ViewGroup){        最后ViewHolder持有人;        如果(convertView == NULL){
            LayoutInflater viewInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = viewInflater.inflate(R.layout.mtopics_childview,父母,假);
            持有人=新ViewHolder();
            holder.text =(TextView中)convertView.findViewById(R.id.mtopicschildtv);
            holder.checkbox =(复选框)convertView.findViewById(R.id.mtopicchildchkbox);            convertView.setTag(保持器);
        }其他{
            支架=(ViewHolder)convertView.getTag();
        }        holder.text.setText(getChild(groupPosition,childPosition)的ToString());        的for(int i = 0; I< children.length;我++){
            ArrayList的<布尔> TMP =新的ArrayList<布尔>(儿童[I]。长度);
            对于(INT J = 0; J<小儿[I]。长度; J ++){
                tmp.add(真);
            }
            check_states.add(TMP);
        }        如果(check_states.get(groupPosition)获得(childPosition)==真){
            holder.checkbox.setChecked(真);
        }其他{holder.checkbox.setChecked(假);
        }        holder.checkbox.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                如果(holder.checkbox.isChecked()){
                    check_states.get(groupPosition).SET(childPosition,真);
                }其他{check_states.get(groupPosition).SET(childPosition,FALSE);
                }
            }
        });        返回convertView;
    }    公共查看getGroupView(最终诠释groupPosition,布尔isExpanded,查看convertView,
            父母的ViewGroup){        ViewHolder持有人;        如果(convertView == NULL){
            LayoutInflater viewInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = viewInflater.inflate(R.layout.mtopics_groupview,父母,假);
            持有人=新ViewHolder();
            holder.text =(TextView中)convertView.findViewById(R.id.mtopicsgrouptv);
            holder.checkbox =(复选框)convertView.findViewById(R.id.cb_group);            convertView.setTag(保持器);
        }其他{
            支架=(ViewHolder)convertView.getTag();
        }        holder.text.setText(getGroup(groupPosition)的ToString());        的for(int i = 0; I< check_states.get(groupPosition).size();我++){
            如果(check_states.get(groupPosition)获得(一)== FALSE){
                Log.d(肉,子复选框是不是都检查!!!);
            }
        }        holder.checkbox.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){            公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
                如果(器isChecked){
                    的for(int i = 0; I< check_states.get(groupPosition).size();我++){
                        check_states.get(groupPosition).SET(我,真);
                    }
                }否则如果(!器isChecked)
                    的for(int i = 0; I< check_states.get(groupPosition).size();我++){
                        check_states.get(groupPosition).SET(I,FALSE);
                    }                notifyDataSetChanged();
            }
        });        返回convertView;
    }    //附加组件
    ArrayList的< ArrayList的<布尔>> check_states =新的ArrayList< ArrayList的<布尔>>(children.length);    静态类ViewHolder {
        TextView的文本;
        复选框复选框;
    }}


解决方案

确定。牢记,我的目标是与复选框级联链,我终于想通了,我在做什么错。我的ArrayList没有被我看到了expandablelistview时填写的原因是因为它默认倒塌,我放在getChildView实例化,这是不被称为呢。因此,移动到固定的getGroupView code座。但要获得组复选框应该像他们的工作,我需要一个新的(更小)的ArrayList只是为那些组列表复选框,这样它们的状态将持续。然后,我相应地设置有线复选框的事件。只是我认为是值得一提的是,在调用notifyDataSetChanged()在点击听众最好的工作或活动改变listerners,因为如果它是在开启或某些条件语句留出再一个运行有它被称为extraneously的风险,这是锁定我的expandablelistview。下面是修改code的重要组成部分,我会改变话题,以准确反映了答案,因为这个问题在不同的方向去了。

 的ArrayList< ArrayList的<布尔>> child_check_states =新的ArrayList< ArrayList的<布尔>>(children.length);
ArrayList的<布尔> group_check_states =新的ArrayList<布尔> (groups.length);静态类ViewHolder {
    TextView的文本;
    复选框复选框;
}公共查看getChildView(最终诠释groupPosition,最终诠释childPosition,布尔isLastChild,
        查看convertView,父母的ViewGroup){    最后ViewHolder持有人;    如果(convertView == NULL){
        LayoutInflater viewInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = viewInflater.inflate(R.layout.mtopics_childview,父母,假);
        持有人=新ViewHolder();
        holder.text =(TextView中)convertView.findViewById(R.id.mtopicschildtv);
        holder.checkbox =(复选框)convertView.findViewById(R.id.mtopicchildchkbox);        convertView.setTag(保持器);
    }其他{
        支架=(ViewHolder)convertView.getTag();
    }    holder.text.setText(getChild(groupPosition,childPosition)的ToString());    如果(child_check_states.get(groupPosition)获得(childPosition)==真){
        holder.checkbox.setChecked(真);
    }其他{holder.checkbox.setChecked(假);
    }    holder.checkbox.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            如果(holder.checkbox.isChecked()){
                child_check_states.get(groupPosition).SET(childPosition,真);
                如果(child_check_states.get(groupPosition)。载有(假)){
                    group_check_states.set(groupPosition,FALSE);
                }其他{
                    group_check_states.set(groupPosition,真);
                }
            }其他{
                child_check_states.get(groupPosition).SET(childPosition,FALSE);
                group_check_states.set(groupPosition,FALSE);
            }
            notifyDataSetChanged();
        }
    });    返回convertView;
}公共查看getGroupView(最终诠释groupPosition,布尔isExpanded,查看convertView,
        父母的ViewGroup){    fillChildArraylists();
    fillGroupArraylist();    最后ViewHolder持有人;    如果(convertView == NULL){
        LayoutInflater viewInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = viewInflater.inflate(R.layout.mtopics_groupview,父母,假);
        持有人=新ViewHolder();
        holder.text =(TextView中)convertView.findViewById(R.id.mtopicsgrouptv);
        holder.checkbox =(复选框)convertView.findViewById(R.id.cb_group);        convertView.setTag(保持器);
    }其他{
        支架=(ViewHolder)convertView.getTag();
    }    holder.text.setText(getGroup(groupPosition)的ToString());    如果((group_check_states.get(groupPosition))==真){
        holder.checkbox.setChecked(真);
    }其他{
        holder.checkbox.setChecked(假);
    }    holder.checkbox.setOnClickListener(新OnClickListener(){        公共无效的onClick(视图v){
            如果(holder.checkbox.isChecked()){
                的for(int i = 0; I< child_check_states.get(groupPosition).size();我++){
                    child_check_states.get(groupPosition).SET(我,真);
                    group_check_states.set(groupPosition,真);
                }
            }其他{
                的for(int i = 0; I< child_check_states.get(groupPosition).size();我++){
                    child_check_states.get(groupPosition).SET(I,FALSE);
                    group_check_states.set(groupPosition,FALSE);
                }
            }
            notifyDataSetChanged();
        }
    });    返回convertView;
}私人无效fillChildArraylists(){
    的for(int i = 0; I< children.length;我++){
        ArrayList的<布尔> TMP =新的ArrayList<布尔>(儿童[I]。长度);
        对于(INT J = 0; J<小儿[I]。长度; J ++){
            tmp.add(真);
        }
        child_check_states.add(TMP);
    }
}私人无效fillGroupArraylist(){
    的for(int i = 0; I< groups.length;我++){
        group_check_states.add(真);
    }
}

It's been a great learning experience but i'm finally almost done with my expandablelistview adapter. It has checkboxes on the groups and childviews so, intuitively, you can disable/enable all the checks on the childviews by action of the group view checkbox. all that's left now is to make group view checkbox correspond to the children views. when i try to it crashes.

    for(int i = 0; i < check_states.get(groupPosition).size(); i++) {
        if (check_states.get(groupPosition).get(i) == false) {
            Log.d ("Meat", "Child checkboxes are not all checked!!!");
        }
    }

I'm very sure now that it is because I call those commands before the arraylist is finished populating --> "check_states" is an arraylist of arraylists that i use to keep the checkstates of the checkboxes.

So my question is very simply how to wait for the arraylist to finish populating before I call my commands. I've looked into what i can, but they seem a bit more involved than what this is worth. I'd rather set up a long-click menu than set up a seperate thread or spare 10 lines of code for something so meager as this.

EDIT: updated with logcat

10-06 19:18:08.204: W/dalvikvm(540): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-06 19:18:08.224: E/AndroidRuntime(540): FATAL EXCEPTION: main
10-06 19:18:08.224: E/AndroidRuntime(540): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-06 19:18:08.224: E/AndroidRuntime(540):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
10-06 19:18:08.224: E/AndroidRuntime(540):  at java.util.ArrayList.get(ArrayList.java:311)
10-06 19:18:08.224: E/AndroidRuntime(540):  at com.mangodeveloper.mcathomie.McatTopicsExpandableListAdapter.getGroupView(McatTopicsExpandableListAdapter.java:166)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:445)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.AbsListView.obtainView(AbsListView.java:1430)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.ListView.makeAndAddView(ListView.java:1745)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.ListView.fillDown(ListView.java:670)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.ListView.fillFromTop(ListView.java:727)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.ListView.layoutChildren(ListView.java:1598)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.AbsListView.onLayout(AbsListView.java:1260)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.os.Looper.loop(Looper.java:123)
10-06 19:18:08.224: E/AndroidRuntime(540):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-06 19:18:08.224: E/AndroidRuntime(540):  at java.lang.reflect.Method.invokeNative(Native Method)
10-06 19:18:08.224: E/AndroidRuntime(540):  at java.lang.reflect.Method.invoke(Method.java:507)
10-06 19:18:08.224: E/AndroidRuntime(540):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-06 19:18:08.224: E/AndroidRuntime(540):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-06 19:18:08.224: E/AndroidRuntime(540):  at dalvik.system.NativeStart.main(Native Method)

Edit again: Here is just about the relevant parts of the expandable adapter

public class McatTopicsExpandableListAdapter extends BaseExpandableListAdapter {

...

public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {
            LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = viewInflater.inflate(R.layout.mtopics_childview, parent, false);  
            holder = new ViewHolder();
            holder.text = (TextView)convertView.findViewById(R.id.mtopicschildtv);
            holder.checkbox = (CheckBox)convertView.findViewById(R.id.mtopicchildchkbox);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(getChild(groupPosition, childPosition).toString());

        for(int i = 0; i < children.length; i++) {
            ArrayList<Boolean> tmp = new ArrayList<Boolean>(children[i].length);
            for(int j = 0; j < children[i].length; j++) {
                tmp.add(true);
            }
            check_states.add(tmp);
        }

        if (check_states.get(groupPosition).get(childPosition) == true) {
            holder.checkbox.setChecked(true);
        }else{ holder.checkbox.setChecked(false);
        }

        holder.checkbox.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {
                if (holder.checkbox.isChecked()) {
                    check_states.get(groupPosition).set(childPosition, true);
                }else{ check_states.get(groupPosition).set(childPosition, false);
                }
            }   
        });

        return convertView;
    }

    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = viewInflater.inflate(R.layout.mtopics_groupview, parent, false);
            holder = new ViewHolder();
            holder.text = (TextView)convertView.findViewById(R.id.mtopicsgrouptv);
            holder.checkbox = (CheckBox)convertView.findViewById(R.id.cb_group);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(getGroup(groupPosition).toString());

        for(int i = 0; i < check_states.get(groupPosition).size(); i++) {
            if (check_states.get(groupPosition).get(i) == false) {
                Log.d ("Meat", "Child checkboxes are not all checked!!!");
            }
        }

        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    for(int i = 0; i < check_states.get(groupPosition).size(); i++) {
                        check_states.get(groupPosition).set(i, true);   
                    }       
                } else if (!isChecked)
                    for(int i = 0; i < check_states.get(groupPosition).size(); i++) {
                        check_states.get(groupPosition).set(i, false);          
                    }

                notifyDataSetChanged();
            }
        }); 

        return convertView;
    }

    //  additional components
    ArrayList<ArrayList<Boolean>> check_states = new ArrayList<ArrayList<Boolean>>(children.length);

    static class ViewHolder {
        TextView text;
        CheckBox checkbox;
    }

}

解决方案

Ok. keeping in mind that my goal was a cascading chain with checkboxes, i finally figured out what i was doing wrong. the reason my arraylist wasn't filled by the time i saw the expandablelistview was because it defaulted collapsed and I placed the instantiation in the getChildView, which was not being called yet. So moving it to the getGroupView code block fixed that. But to get group checkboxes working like they should, I needed a new (much smaller) arraylist just for those group list checkboxes so that their states would persist. Then I set wired check box events accordingly. Just something i thought was worth noting was that calling notifyDataSetChanged() worked best in click listeners or event changed listerners because if it was left out in the open or some conditional statement then one runs the risk of having it being called extraneously, which was locking up my expandablelistview. Below is important parts of the revised code and i'll change the subject to accurately reflect the answer since this question went in a different direction.

ArrayList<ArrayList<Boolean>> child_check_states = new ArrayList<ArrayList<Boolean>>(children.length);
ArrayList<Boolean> group_check_states = new ArrayList<Boolean> (groups.length);

static class ViewHolder {
    TextView text;
    CheckBox checkbox;
}

public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
        View convertView, ViewGroup parent) {

    final ViewHolder holder;

    if (convertView == null) {
        LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = viewInflater.inflate(R.layout.mtopics_childview, parent, false);  
        holder = new ViewHolder();
        holder.text = (TextView)convertView.findViewById(R.id.mtopicschildtv);
        holder.checkbox = (CheckBox)convertView.findViewById(R.id.mtopicchildchkbox);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.text.setText(getChild(groupPosition, childPosition).toString());

    if (child_check_states.get(groupPosition).get(childPosition) == true) {
        holder.checkbox.setChecked(true);   
    }else{ holder.checkbox.setChecked(false);
    }

    holder.checkbox.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {
            if (holder.checkbox.isChecked()) {
                child_check_states.get(groupPosition).set(childPosition, true);
                if (child_check_states.get(groupPosition).contains(false)) {
                    group_check_states.set(groupPosition, false);
                }else{
                    group_check_states.set(groupPosition, true);
                }
            }else{ 
                child_check_states.get(groupPosition).set(childPosition, false);
                group_check_states.set(groupPosition, false);
            }
            notifyDataSetChanged();
        }   
    });

    return convertView;
}

public View getGroupView(final int groupPosition, boolean isExpanded, View convertView,
        ViewGroup parent) {

    fillChildArraylists();
    fillGroupArraylist();

    final ViewHolder holder;

    if (convertView == null) {
        LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = viewInflater.inflate(R.layout.mtopics_groupview, parent, false);
        holder = new ViewHolder();
        holder.text = (TextView)convertView.findViewById(R.id.mtopicsgrouptv);
        holder.checkbox = (CheckBox)convertView.findViewById(R.id.cb_group);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.text.setText(getGroup(groupPosition).toString());

    if ((group_check_states.get(groupPosition)) == true) {
        holder.checkbox.setChecked(true);
    } else {
        holder.checkbox.setChecked(false);
    }

    holder.checkbox.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (holder.checkbox.isChecked()) {
                for(int i = 0; i < child_check_states.get(groupPosition).size(); i++) {
                    child_check_states.get(groupPosition).set(i, true);
                    group_check_states.set(groupPosition, true);
                }
            } else {
                for(int i = 0; i < child_check_states.get(groupPosition).size(); i++) {
                    child_check_states.get(groupPosition).set(i, false);
                    group_check_states.set(groupPosition, false);
                }
            }
            notifyDataSetChanged();
        }
    });

    return convertView;
}

private void fillChildArraylists () {
    for(int i = 0; i < children.length; i++) {
        ArrayList<Boolean> tmp = new ArrayList<Boolean>(children[i].length);
        for(int j = 0; j < children[i].length; j++) {
            tmp.add(true);
        }
        child_check_states.add(tmp);
    }
}

private void fillGroupArraylist () {
    for(int i = 0; i < groups.length; i++) {
        group_check_states.add(true);
    }
}

这篇关于接线起来expandablelistview级联复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆