안드로이드의 ExpandableListView를 사용하는 데 문제가 있습니다. 시스템에 설치된 패키지 이름을 그룹으로 사용하고 노출 된 활동을 하위로 사용하는 확인란으로 간단한 확장 가능한 목록을 작성하려고합니다.ExpandableListView IndexOutOfBoundException
그것의 모든 난 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)
나는 조금 멍청하다. ( 왜 metod getChildrenCount가 나를 묶어 놓은 예외에서 풀어 주는지 이해가 안된다. childPosition
이기 때문이다 그래서 여기 당신이 get
호출 할 때
public int getChildrenCount(int childPosition) {
return activityName.get(childPosition).size();
}
및 값 : 다음 코드에 ArrayList
변수 activity
의 크기보다 큰 int
값을 사용 할 수있다
귀하의 패키지 이름 크기는 85입니까? 그렇다면 activityName.size를 반환하도록 getGroupCount() 메서드를 변경해야합니다. –