这是一次很棒的学习经历,但我终于完成了我的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完成填充之前调用了这些命令 - &gt; “check_states”是一个arraylists的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;
}
}
答案 0 :(得分:1)
您正在检查其他阵列的长度...将您的循环条件更改为:
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!!!");
}
}
答案 1 :(得分:0)
确定。请记住,我的目标是带有复选框的级联链,我终于弄清楚我做错了什么。当我看到expandablelistview时,我的arraylist没有被填充的原因是因为它默认崩溃了,我把实例化放在了getChildView中,但还没有被调用。所以将它移动到getGroupView代码块修复了这个问题。但是为了让组复选框像它们应该的那样工作,我需要一个新的(小得多)arraylist只为那些组列表复选框,以便它们的状态将持续存在。然后我相应地设置有线复选框事件。我认为值得注意的是调用notifyDataSetChanged()在click侦听器或事件更改Listerners中效果最好,因为如果它在open或者某些条件语句中被遗漏,那么就会冒着被外部调用的风险,这就是锁定我的expandablelistview。以下是修订后的代码的重要部分,我将更改主题以准确反映答案,因为这个问题的方向不同。
ArrayList<ArrayList<Boolean>> child_check_states = new ArrayList<ArrayList<Boolean>>(children.length);
ArrayList<Boolean> group_check_states = new ArrayList<Boolean> (groups.length);
static class ViewHolder {
TextView text;
CheckBox checkbox;
}
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());
if (child_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()) {
child_check_states.get(groupPosition).set(childPosition, true);
if (child_check_states.get(groupPosition).contains(false)) {
group_check_states.set(groupPosition, false);
}else{
group_check_states.set(groupPosition, true);
}
}else{
child_check_states.get(groupPosition).set(childPosition, false);
group_check_states.set(groupPosition, false);
}
notifyDataSetChanged();
}
});
return convertView;
}
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
fillChildArraylists();
fillGroupArraylist();
final 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());
if ((group_check_states.get(groupPosition)) == true) {
holder.checkbox.setChecked(true);
} else {
holder.checkbox.setChecked(false);
}
holder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (holder.checkbox.isChecked()) {
for(int i = 0; i < child_check_states.get(groupPosition).size(); i++) {
child_check_states.get(groupPosition).set(i, true);
group_check_states.set(groupPosition, true);
}
} else {
for(int i = 0; i < child_check_states.get(groupPosition).size(); i++) {
child_check_states.get(groupPosition).set(i, false);
group_check_states.set(groupPosition, false);
}
}
notifyDataSetChanged();
}
});
return convertView;
}
private void fillChildArraylists () {
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);
}
child_check_states.add(tmp);
}
}
private void fillGroupArraylist () {
for(int i = 0; i < groups.length; i++) {
group_check_states.add(true);
}
}