0

我在 android 中使用 ExpandableListView 时遇到问题。我正在尝试使用安装在系统上的包名称作为组来构建一个带有复选框的简单可扩展列表,并将活动作为子项公开。

在我尝试在 85 之后扩展元素之前,这一切都很好。这是代码:

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


    }


}

适配器 :

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) {}


    }

日志猫:

 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)

我有点菜鸟:(我真的不明白为什么方法 getChildrenCount 让我超出了界限。在此先感谢

4

2 回答 2

3

您可能在以下代码中使用了int大于ArrayList变量大小的值:activity

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

因此,当您调用get并且值childPosition大于您得到的大小时IndexOutOfBoundException,请确保您调用 get 时的int值较小。

于 2013-02-04T10:37:37.237 回答
1

如果 aInfos == null 怎么办?您最终将添加 pInfo.packageName 而不是复选框。这可能会导致 packageName 和活动列表之间的大小不同。

        对于(PackageInfo pInfo:pInfos){
          groupNames.add(pInfo.packageName);
          ActivityInfo[] aInfos = pInfo.activities;
          ArrayList cb = new ArrayList();
          如果(aInfos!= null){
                对于(活动信息活动信息:aInfos){
                    字符串名称 = activityInfo.name;
                    字符串 str[] = name.split("\\.");
                    cb.add(new CheckBoxes(str[str.length-1], false));

                }
                cbs.add(cb);
          }
        }
于 2013-02-04T10:47:02.773 回答