我在安卓系统中使用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给了我一个例外。提前感谢
发布于 2013-02-04 10:37:37
在下面的代码中,您可能使用的int值大于ArrayList变量activity的大小:
public int getChildrenCount(int childPosition) {
return activityName.get( childPosition).size();
}因此,在这里,当您调用get,并且childPosition的值大于您得到的IndexOutOfBoundException的大小时,请确保使用更小的int值调用get。
https://stackoverflow.com/questions/14684823
复制相似问题