根据这个伟大的答案Android ExpandableListView with Checkbox, Controlling checked state我创建了我的课但是我收到一个错误,LogCat ArrayList.IndexOutOfBoundsException
mMyClass
public ArrayList<String> groups;
public ArrayList<ArrayList<ArrayList<String>>> childs;
..........
ArrayList < ArrayList < Integer >> check_states =new ArrayList < ArrayList < Integer>>();
public class myExpandableAdapter extends BaseExpandableListAdapter {
private ArrayList<String> groups;
private ArrayList<ArrayList<ArrayList<String>>> children;
private final Context context ;
public myExpandableAdapter(Context context,ArrayList<String> groups, ArrayList<ArrayList<ArrayList<String>>> children) {
this.context = context;
this.groups = groups;
this.children = children; }
public void setChildrenAndValues(ArrayList< ArrayList< ArrayList< String>>> c) {
this.children = c;
//initialize the states to all 0;
for(int i = 0; i < c.size(); i++) {
ArrayList<Integer> tmp = new ArrayList<Integer>();
for(int j = 0; j < c.size(); j++) {
tmp.add(0);
}
check_states.add(tmp);
}
}
@Override
public boolean areAllItemsEnabled()
{
return true;
}
@Override
public ArrayList<String> getChild(int groupPosition, int childPosition) {
return children.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {
String child = (String) ((ArrayList<String>)getChild(groupPosition, childPosition)).get(0);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.exp_list_child, null);
}
TextView childtxt = (TextView) convertView.findViewById(R.id.list_child1);
childtxt.setText(child);
///
final View tick = findViewById(R.id.checkBox1);
if(check_states.get(groupPosition).get(childPosition) == 1)
tick.setVisibility(View.VISIBLE);
else
tick.setVisibility(View.GONE);
convertView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
check_states.get(groupPosition).set(childPosition, 1);
Toast.makeText(getApplicationContext(),""+ check_states, Toast.LENGTH_SHORT).show();
tick.setVisibility(View.VISIBLE);
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return children.get(groupPosition).size();
}
@Override
public String getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String group = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.exp_list_group, null);
}
TextView grouptxt = (TextView) convertView.findViewById(R.id.ListHeader);
grouptxt.setText(group);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
public void loadData(){
groups= new ArrayList<String>();
childs= new ArrayList<ArrayList<ArrayList<String>>>();
int proion_id= getIntent().getExtras().getInt("proionId");
int length = dbAdapter.getDetailsOf(proion_id).size();
if(length >0) {
for (int i=0; i< length; i++)
{
childs.add(new ArrayList<ArrayList<String>>());
childs.get(i).add(new ArrayList<String>());
childs.get(i).get(0).add("A");
childs.get(i).add(new ArrayList<String>());
childs.get(i).get(1).add("B");
childs.get(i).add(new ArrayList<String>());
childs.get(i).get(2).add("C");
}
} else
{childs.add(new ArrayList<ArrayList<String>>());
childs.get(0).add(new ArrayList<String>());
childs.get(0).get(0).add("NO");}
}
}
logcat的
> 07-03 03:42:57.168: E/AndroidRuntime(1644): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
07-03 03:42:57.168: E/AndroidRuntime(1644): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
07-03 03:42:57.168: E/AndroidRuntime(1644): at java.util.ArrayList.get(ArrayList.java:308)
07-03 03:42:57.168: E/AndroidRuntime(1644): at com.techblogon.loginexample.ItemComments$myExpandableAdapter.getChildView(ItemComments.java:156)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.ListView.onMeasure(ListView.java:1175)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.View.measure(View.java:16497)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.View.measure(View.java:16497)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.View.measure(View.java:16497)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.View.measure(View.java:16497)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.View.measure(View.java:16497)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.View.measure(View.java:16497)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-03 03:42:57.168: E/AndroidRuntime(1644): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.View.measure(View.java:16497)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.Choreographer.doFrame(Choreographer.java:544)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.os.Handler.handleCallback(Handler.java:733)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.os.Handler.dispatchMessage(Handler.java:95)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.os.Looper.loop(Looper.java:136)
07-03 03:42:57.168: E/AndroidRuntime(1644): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-03 03:42:57.168: E/AndroidRuntime(1644): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 03:42:57.168: E/AndroidRuntime(1644): at java.lang.reflect.Method.invoke(Method.java:515)
07-03 03:42:57.168: E/AndroidRuntime(1644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-03 03:42:57.168: E/AndroidRuntime(1644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-03 03:42:57.168: E/AndroidRuntime(1644): at dalvik.system.NativeStart.main(Native Method)
提前致谢
答案 0 :(得分:0)
您可能(不)看到您的日志告诉您,当getChildView内部的大小实际为零时,您正在访问数组的第一个元素。
我不知道为什么你的数组没有加载数据但是如果你在访问数组之前添加一个检查它可能会有所帮助:
if (getChild(groupPosition, childPosition) > 0)
String child = getChild(groupPosition, childPosition).get(0);
如果您的数组未完全初始化,则会抛出一个不同的错误,表示您正在尝试访问NullPointer。