ExpandableListView - 展开和折叠组

时间:2013-03-27 07:38:27

标签: android expandablelistview

如果组ID在我所拥有的arraylist中,我试图展开和折叠组。

我就是这样做的:

final Group group = (Group) getGroup(groupPosition);   
ExpandableListView eLV = (ExpandableListView) parent;
if ( subscriptions != null ) {
    if ( subscriptions.contains(group.getID()) ) {
        ((CheckBox) v.findViewById(R.id.check_channel)).setChecked(true);
        eLV.expandGroup(groupPosition);
    } else {
        ((CheckBox) v.findViewById(R.id.check_channel)).setChecked(false);
        eLV.collapseGroup(groupPosition);
    }
}

首次加载组并扩展正确的组时,它会起作用。当我按下该组时,它会按预期崩溃。

但是,当我想要扩展群组或选择群组时,应用会崩溃。仅当我从else块中删除部分 eLV.collapseGroup(groupPosition); 时,才会再次使用它。但后来由于显而易见的原因我不能崩溃..

这是一个例外:

03-27 09:29:11.059: W/dalvikvm(21674): threadid=1: thread exiting with uncaught exception (group=0x40bec1f8)
03-27 09:29:11.067: E/AndroidRuntime(21674): FATAL EXCEPTION: main
03-27 09:29:11.067: E/AndroidRuntime(21674): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-27 09:29:11.067: E/AndroidRuntime(21674):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at java.util.ArrayList.get(ArrayList.java:304)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:225)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.AbsListView.obtainView(AbsListView.java:2210)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.ListView.onMeasure(ListView.java:1155)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1027)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:565)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2240)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.View.measure(View.java:12775)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1092)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.os.Looper.loop(Looper.java:137)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at android.app.ActivityThread.main(ActivityThread.java:4514)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at java.lang.reflect.Method.invokeNative(Native Method)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at java.lang.reflect.Method.invoke(Method.java:511)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-27 09:29:11.067: E/AndroidRuntime(21674):    at dalvik.system.NativeStart.main(Native Method)

完整的功能:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,ViewGroup parent) { 
    View v;
    if (convertView == null) {
        v = newGroupView(isExpanded, parent);
    } else {
        v = convertView;
    }



    final Group group = (Group) getGroup(groupPosition);   
    ExpandableListView eLV = (ExpandableListView) parent;
    if ( subscriptions != null ) {
        if ( subscriptions.contains(group.getID()) ) {
            ((CheckBox) v.findViewById(R.id.check_channel)).setChecked(true);
            eLV.expandGroup(groupPosition);
        } else {
            ((CheckBox) v.findViewById(R.id.check_channel)).setChecked(false);
            eLV.collapseGroup(groupPosition);
        }
    }

    if ( isExpanded ) {
        if ( subscriptions.contains(group.getID()) == false ) {
            subscriptions.add(group.getID());
        }
    } else {
        for (int i=0;i<subscriptions.size();i++) {
            if ( subscriptions.get(i).equals(group.getID())) {
                subscriptions.remove(i);
            }
        }
    }

    bindGroupView(groupPosition, isExpanded, v, parent);
    return v;
}

0 个答案:

没有答案