我在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) {}
}
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)
我有点小便:( 我真的不明白为什么metod getChildrenCount会让我失去异常。 提前致谢
答案 0 :(得分:3)
您可能在以下代码中使用的int
值大于ArrayList
变量activity
的大小:
public int getChildrenCount(int childPosition) {
return activityName.get( childPosition).size();
}
因此,当您致电get
并且childPosition
的值大于您获得的IndexOutOfBoundException
时,请确保使用较小的int
来调用get值。
答案 1 :(得分:1)
如果aInfos == null怎么办? 您最终会添加pInfo.packageName而不是复选框。 这可能导致packageName和活动列表之间的大小不同。
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 ); } }