0

这是一次很棒的学习经历,但我的 expandablelistview 适配器终于快用完了。它在组和子视图上有复选框,因此,直观地,您可以通过组视图复选框的操作禁用/启用对子视图的所有检查。现在剩下的就是使组视图复选框对应于子视图。当我尝试它崩溃。

    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!!!");
        }
    }

我现在非常确定,这是因为我在 arraylist 完成填充之前调用了这些命令——>“check_states”是一个 arraylist 的arraylist,我用来保持复选框的检查状态。

所以我的问题很简单,在我调用命令之前如何等待 arraylist 完成填充。我已经研究了我能做的事情,但他们似乎比这更有价值。我宁愿设置一个长按菜单,也不愿设置一个单独的线程或为如此微薄的东西设置 10 行代码。

编辑:用 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)

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

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;
    }

}
4

2 回答 2

1

您正在检查不同数组的长度...将循环条件更改为:

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!!!");
    }
}
于 2012-10-06T16:29:55.413 回答
0

行。请记住,我的目标是带有复选框的级联链,我终于弄清楚我做错了什么。当我看到可扩展列表视图时,我的数组列表没有被填充的原因是因为它默认折叠并且我将实例化放在尚未被调用的 getChildView 中。所以将它移动到 getGroupView 代码块修复了这个问题。但是为了让组复选框按应有的方式工作,我需要一个新的(小得多的)数组列表,仅用于这些组列表复选框,以便它们的状态持续存在。然后我相应地设置有线复选框事件。我认为值得注意的是调用 notifyDataSetChanged() 在单击侦听器或事件更改侦听器中效果最好,因为如果它在打开或某些条件语句中被遗漏,那么就会冒着被外部调用的风险,这是锁定打开我的可扩展列表视图。以下是修改后的代码的重要部分,我将更改主题以准确反映答案,因为这个问题朝着不同的方向发展。

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);
    }
}
于 2012-10-07T22:32:42.370 回答