blastbeng
blastbeng

Reputation: 191

ExpandableListView IndexOutOfBoundException

i have a problem using the ExpandableListView in android. I am trying to build a simple expandable list with checkboxes using package Names installed on the system as group, and exposed activities as childrens.

It's all working good till i try to expand elements after 85. Here's the code:

 public class ResultCheckbox extends ExpandableListActivity {
    CheckBoxAdapter mAdapter;
    ExpandableListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




        setContentView(R.layout.check_list_layout);

        ArrayList<ArrayList<CheckBoxes>> cbs = new ArrayList<ArrayList<CheckBoxes>>(); 

        ArrayList<String> groupNames = new ArrayList<String>();
        List<PackageInfo> pInfos = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);
        for (PackageInfo pInfo : pInfos) {
          groupNames.add(pInfo.packageName);
          ActivityInfo[] aInfos = pInfo.activities;
          ArrayList<CheckBoxes> cb = new ArrayList<CheckBoxes>();
          if (aInfos != null) {
                for (ActivityInfo activityInfo : aInfos) {
                    String name = activityInfo.name;
                    String str[] = name.split("\\.");
                    cb.add( new CheckBoxes( str[str.length-1], false ) ); 

                }
                cbs.add( cb );
          }
        }

        mAdapter = new CheckBoxAdapter( this,groupNames, cbs );
        setListAdapter( mAdapter );


    }


}

Adapter :

public class CheckBoxAdapter extends BaseExpandableListAdapter {
    private ArrayList<String> packageName;
    private ArrayList<ArrayList<CheckBoxes>> activityName;
    private LayoutInflater inflater;

    public CheckBoxAdapter(Context context, 
                        ArrayList<String> packageName,
                        ArrayList<ArrayList<CheckBoxes>> activityName ) { 
        this.packageName = packageName;
        this.activityName = activityName;
        inflater = LayoutInflater.from( context );
    }

    public Object getChild(int groupPosition, int childPosition) {
        return activityName.get( groupPosition ).get( childPosition );
    }

    public long getChildId(int groupPosition, int childPosition) {
        return (long)( groupPosition*1024+childPosition );  // Max 1024 children per group
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.child_row, parent, false); 
        CheckBoxes c = (CheckBoxes)getChild( groupPosition, childPosition );
        TextView rgb = (TextView)v.findViewById( R.id.checkBox );
        if( rgb != null )
            rgb.setText( c.getActivityName() );
        CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
        cb.setChecked( c.getState() );
        return v;
    }

    public int getChildrenCount(int childPosition) {
        return activityName.get( childPosition).size();
    }

    public Object getGroup(int groupPosition) {
        return packageName.get( groupPosition );        
    }

    public int getGroupCount() {
        return packageName.size();
    }

    public long getGroupId(int groupPosition) {
        return (long)( groupPosition*1024 );  // To be consistent with getChildId
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.group_row, parent, false); 
        String gt = (String)getGroup( groupPosition );
        TextView pkgGroup = (TextView)v.findViewById( R.id.childname );
        if( gt != null )
            pkgGroup.setText( gt );
        return v;
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    } 

    public void onGroupCollapsed (int groupPosition) {} 
    public void onGroupExpanded(int groupPosition) {}


    }

LogCat :

 02-04 11:17:00.379: E/AndroidRuntime(4514): FATAL EXCEPTION: main
02-04 11:17:00.379: E/AndroidRuntime(4514): java.lang.IndexOutOfBoundsException: Invalid index 91, size is 85
02-04 11:17:00.379: E/AndroidRuntime(4514):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at java.util.ArrayList.get(ArrayList.java:304)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at com.blast.makeyournotification.adapter.CheckBoxAdapter.getChildrenCount(CheckBoxAdapter.java:53)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:563)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:688)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:562)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at android.os.Handler.handleCallback(Handler.java:725)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at android.os.Looper.loop(Looper.java:137)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at android.app.ActivityThread.main(ActivityThread.java:5191)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at java.lang.reflect.Method.invoke(Method.java:511)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-04 11:17:00.379: E/AndroidRuntime(4514):     at dalvik.system.NativeStart.main(Native Method)

I am a little noobish :( I really don't understand why the metod getChildrenCount is givin me out of bound exception. Thanks in advance

Upvotes: 0

Views: 1754

Answers (2)

Xval
Xval

Reputation: 938

What if aInfos == null ? You'll end up adding the pInfo.packageName but not the checkbox. This might result in different size bettween packageName and activities lists.

        for (PackageInfo pInfo : pInfos) {
          groupNames.add(pInfo.packageName);
          ActivityInfo[] aInfos = pInfo.activities;
          ArrayList cb = new ArrayList();
          if (aInfos != null) {
                for (ActivityInfo activityInfo : aInfos) {
                    String name = activityInfo.name;
                    String str[] = name.split("\\.");
                    cb.add( new CheckBoxes( str[str.length-1], false ) ); 

                }
                cbs.add( cb );
          }
        }

Upvotes: 1

Abubakkar
Abubakkar

Reputation: 15654

You may be using an int value that is greater than the size of your ArrayList variable activity in the following code:

public int getChildrenCount(int childPosition) {
    return activityName.get( childPosition).size();
}

So here when you call get and the value for childPosition is greater than the size than you get IndexOutOfBoundException so make sure, you call get with a smaller int value.

Upvotes: 3

Related Questions