这是一次很棒的学习经历,但我的 expandablelistview 适配器终于快用完了。它在组和子视图上有复选框,因此,直观地,您可以通过组视图复选框的操作禁用/启用对子视图的所有检查。现在剩下的就是使组视图复选框对应于子视图。当我尝试它崩溃。
for(int i = 0; i < check_states.get(groupPosition).size(); i++) {
if (check_states.get(groupPosition).get(i) == false) {
Log.d ("Meat", "Child checkboxes are not all checked!!!");
}
}
我现在非常确定,这是因为我在 arraylist 完成填充之前调用了这些命令——>“check_states”是一个 arraylist 的arraylist,我用来保持复选框的检查状态。
所以我的问题很简单,在我调用命令之前如何等待 arraylist 完成填充。我已经研究了我能做的事情,但他们似乎比这更有价值。我宁愿设置一个长按菜单,也不愿设置一个单独的线程或为如此微薄的东西设置 10 行代码。
编辑:用 logcat 更新
10-06 19:18:08.204: W/dalvikvm(540): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-06 19:18:08.224: E/AndroidRuntime(540): FATAL EXCEPTION: main
10-06 19:18:08.224: E/AndroidRuntime(540): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-06 19:18:08.224: E/AndroidRuntime(540): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
10-06 19:18:08.224: E/AndroidRuntime(540): at java.util.ArrayList.get(ArrayList.java:311)
10-06 19:18:08.224: E/AndroidRuntime(540): at com.mangodeveloper.mcathomie.McatTopicsExpandableListAdapter.getGroupView(McatTopicsExpandableListAdapter.java:166)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:445)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.ListView.makeAndAddView(ListView.java:1745)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.ListView.fillDown(ListView.java:670)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.ListView.fillFromTop(ListView.java:727)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.ListView.layoutChildren(ListView.java:1598)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.view.View.layout(View.java:7175)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.os.Looper.loop(Looper.java:123)
10-06 19:18:08.224: E/AndroidRuntime(540): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-06 19:18:08.224: E/AndroidRuntime(540): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 19:18:08.224: E/AndroidRuntime(540): at java.lang.reflect.Method.invoke(Method.java:507)
10-06 19:18:08.224: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-06 19:18:08.224: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-06 19:18:08.224: E/AndroidRuntime(540): at dalvik.system.NativeStart.main(Native Method)
再次编辑:这只是可扩展适配器的相关部分
public class McatTopicsExpandableListAdapter extends BaseExpandableListAdapter {
...
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = viewInflater.inflate(R.layout.mtopics_childview, parent, false);
holder = new ViewHolder();
holder.text = (TextView)convertView.findViewById(R.id.mtopicschildtv);
holder.checkbox = (CheckBox)convertView.findViewById(R.id.mtopicchildchkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(getChild(groupPosition, childPosition).toString());
for(int i = 0; i < children.length; i++) {
ArrayList<Boolean> tmp = new ArrayList<Boolean>(children[i].length);
for(int j = 0; j < children[i].length; j++) {
tmp.add(true);
}
check_states.add(tmp);
}
if (check_states.get(groupPosition).get(childPosition) == true) {
holder.checkbox.setChecked(true);
}else{ holder.checkbox.setChecked(false);
}
holder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (holder.checkbox.isChecked()) {
check_states.get(groupPosition).set(childPosition, true);
}else{ check_states.get(groupPosition).set(childPosition, false);
}
}
});
return convertView;
}
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = viewInflater.inflate(R.layout.mtopics_groupview, parent, false);
holder = new ViewHolder();
holder.text = (TextView)convertView.findViewById(R.id.mtopicsgrouptv);
holder.checkbox = (CheckBox)convertView.findViewById(R.id.cb_group);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(getGroup(groupPosition).toString());
for(int i = 0; i < check_states.get(groupPosition).size(); i++) {
if (check_states.get(groupPosition).get(i) == false) {
Log.d ("Meat", "Child checkboxes are not all checked!!!");
}
}
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
for(int i = 0; i < check_states.get(groupPosition).size(); i++) {
check_states.get(groupPosition).set(i, true);
}
} else if (!isChecked)
for(int i = 0; i < check_states.get(groupPosition).size(); i++) {
check_states.get(groupPosition).set(i, false);
}
notifyDataSetChanged();
}
});
return convertView;
}
// additional components
ArrayList<ArrayList<Boolean>> check_states = new ArrayList<ArrayList<Boolean>>(children.length);
static class ViewHolder {
TextView text;
CheckBox checkbox;
}
}