expandablelistview ArrayIndexOutOfBoundsException

1.1k views Asked by At

I am trying to implement an ExpandableListView with 2 different layouts, so far it took me a while to actually make 1 viewgroup to use a layout and the 2 other viewgroups use the other. And so far the appearance is what's needed, but when i click twice on the same viewgroup or on two different viewgroups the app just crashes with arrayoutofbounds error. this is my code

@Override
public int getChildType(int groupPosition, int childPosition) {

    int result;
    if (groupPosition == 0)
    {
        Log.d("getChildType","Parent " + groupPosition + " detected. returning 0");
        result = 0;
    }
    else
    {
        Log.d("getChildType","Parent " + groupPosition + " detected. returning 1");
        result = 1;
    }
    return result;
}

@Override
public View getChildView(final int parentPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    Log.d("status","parent : "+parentPosition + "child : " + childPosition + "View : "+convertView + "ViewGroup : "+parent);
    final String child_title = (String) getChild(parentPosition, childPosition);
    int itemType = 5;
    if (convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemType = getChildType(parentPosition,childPosition);

        switch (itemType)
        {
            case 0:
                convertView = inflater.inflate(R.layout.activities2_child_layout, parent, false);
                break;
            case 1:
                convertView = inflater.inflate(R.layout.activities2_child2_layout, parent, false);
                break;
        }
    }
    if (itemType == 0)
    {
        TextView child_textview = (TextView) convertView.findViewById(R.id.activities2_child_txt);
        final ImageView img_Likes = (ImageView) convertView.findViewById(R.id.activities2_img_like);
        final ImageView img_Dislikes = (ImageView) convertView.findViewById(R.id.activities2_img_dislike);
        child_textview.setText(child_title);
    }
    else if(itemType == 1)
    {
        TextView child_textview = (TextView) convertView.findViewById(R.id.activities2_child2_txt);
        child_textview.setText(child_title);
    }



    return convertView;
}

Error

    06-15 11:54:14.753  29578-29578/android.omar.com.slidingtab E/AndroidRuntime﹕ FATAL EXCEPTION: main
        java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
06-15 12:02:53.343  31025-31025/android.omar.com.slidingtab W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41eb1378)
06-15 12:02:53.393  31025-31025/android.omar.com.slidingtab E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
            at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6444)
            at android.widget.ListView.layoutChildren(ListView.java:1603)
            at android.widget.AbsListView.onLayout(AbsListView.java:2106)
            at android.view.View.layout(View.java:13923)
            at android.view.ViewGroup.layout(ViewGroup.java:4472)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:953)
            at android.view.View.layout(View.java:13923)
            at android.view.ViewGroup.layout(ViewGroup.java:4472)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:460)
            at android.view.View.layout(View.java:13923)
            at android.view.ViewGroup.layout(ViewGroup.java:4472)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1687)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1545)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1450)
            at android.view.View.layout(View.java:13923)
            at android.view.ViewGroup.layout(ViewGroup.java:4472)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:460)
            at android.view.View.layout(View.java:13923)
            at android.view.ViewGroup.layout(ViewGroup.java:4472)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1687)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1545)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1450)
            at android.view.View.layout(View.java:13923)
            at android.view.ViewGroup.layout(ViewGroup.java:4472)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:460)
            at android.view.View.layout(View.java:13923)
            at android.view.ViewGroup.layout(ViewGroup.java:4472)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1876)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1697)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4223)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Choreographer.java:525)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

Hoping for a fast answer. Thanks a lot in advance
Omar

1

There are 1 answers

1
M.Khouli On BEST ANSWER

I think the you should override getChildTypeCount() and return 2

   @Override
   public int getChildTypeCount(){
        return 2;
    }

Hope this will help.