【问题标题】:wiring up expandablelistview cascading checkboxes连接可扩展列表视图级联复选框
【发布时间】:2012-10-06 15:41:33
【问题描述】:

这是一次很棒的学习经历,但我的 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!!!");
        }
    }

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

所以我的问题很简单,就是在调用命令之前如何等待 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;
    }

}

【问题讨论】:

  • 您应该添加堆栈跟踪,但 logcat 除外。是什么让使用 boolean 值填充列表如此耗时?
  • 怎么样,boolean allChildBoxesChecked = !check_states.get(groupPosition).contains(false);但是,这并不能解决您的问题。请发布 logcat 和代码
  • 那里。我用 logcat 更新了它。我有理由相信它一定是一些毫秒差异的时间问题,因为如果我将代码放在 arraylist 填充方法之后,它可以顺利工作。不过,感谢您的优雅花絮,Tamir。
  • 这是: if (check_states.get(groupPosition).get(i) == false) { McatTopicsExpandableListAdapter 中的第 183 行?如果是这样,则该数组似乎完全为空(大小为 0)
  • 如果不在arraylist populatoin方法之后,你把它放在哪里?

标签: android multithreading android-listview android-asynctask android-view


【解决方案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!!!");
    }
}

【讨论】:

  • 很遗憾这不起作用,但我想你已经向我展示了我一直在使用接近但不完全正确的变量
  • 我承认我不确定您要做什么,我只是阅读了 logcat 并纠正了错误。 check_states.get(groupPosition) 的大小仍然为 0。但是我认为如果没有看到更多代码,我不会提供任何更具体的帮助。
【解决方案2】:

好的。请记住,我的目标是带有复选框的级联链,我终于弄清楚我做错了什么。当我看到可扩展列表视图时,我的数组列表没有被填充的原因是因为它默认折叠并且我将实例化放在尚未被调用的 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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    相关资源
    最近更新 更多