我有一个自定义的ExpandableListView,如果该组为空,则想要以自动方式删除组项。问题是:当我删除最后一个孩子后删除组时,虽然getCount()返回正确的数字,但下面的子项目组将加倍!
例如:
删除前:
g1={c1, c2}, g2={c3, c4}
删除c1后,c2:
g2={c3, c4, c3, c4}
完整的适配器代码:
package base.util.ui.listview;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
public abstract class ExpandableListAdapter extends BaseExpandableListAdapter {
private static final String TAG = ExpandableListAdapter.class
.getSimpleName();
public List<IGroup> list;
private Hashtable<String, Integer> table;
public ExpandableListAdapter() {
this(null);
}
public ExpandableListAdapter(IGroup[] groups) {
list = new ArrayList<IGroup>();
table = new Hashtable<String, Integer>();
for (int i = 0; groups != null && i < groups.length; i++) {
addGroup(groups[i]);
}
}
public int addGroup(IGroup group) {
list.add(group);
table.put(group.getId(), list.indexOf(group));
notifyDataSetChanged();
return list.indexOf(group);
}
public void addChild(IGroup group, IChild child) {
Integer position = table.get(group.getId());
if (position == null) {
position = addGroup(group);
}
if (position != -1) {
getGroup(position).addChild(child);
notifyDataSetChanged();
}
}
public void removeGroup(int groupPosition) {
IGroup group = list.remove(groupPosition);
table.remove(group.getId());
notifyDataSetChanged();
}
public void removeChild(int groupPosition, int childPosition) {
try {
IGroup group = getGroup(groupPosition);
group.removeChild(childPosition);
notifyDataSetChanged();
if (group.getChildCount() == 0) {
removeGroup(groupPosition);// !
}
Log.i(TAG, "removeChild(): " + getChildrenCount());
} catch (Exception e) {
e.printStackTrace();
}
}
public void clear() {
list.clear();
table.clear();
notifyDataSetChanged();
}
public int getChildrenCount() {
int result = 0;
for (int i = 0; i < getGroupCount(); i++) {
result += getChildrenCount(i);
}
return result;
}
public boolean hasStableIds() {
return true;
}
public IGroup getGroup(int groupPosition) {
return list.get(groupPosition);
}
public IChild getChild(int groupPosition, int childPosition) {
return getGroup(groupPosition).getChild(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return -1;
// return getChild(groupPosition, childPosition).childId;
}
public int getChildrenCount(int groupPosition) {
return getGroup(groupPosition).getChildCount();
}
public int getGroupCount() {
return list.size();
}
public long getGroupId(int groupPosition) {
return 0;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public abstract View getGroupView(final int groupPosition,
boolean isExpanded, View convertView, ViewGroup parent);
public abstract View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent);
public void traverse(ITraverse task) {
for (int g = getGroupCount() - 1; g >= 0; g--) {
for (int c = getGroup(g).getChildCount() - 1; c >= 0; c--) {
task.onTraverse(g, c);
}
}
}
}
请注意,在我从removeChild()注释removeGroup()之后,这个问题再也不会发生了。所以有人请告诉我如何正确地做到这一点? THX!
答案 0 :(得分:1)
更改为此,并且有效:
public long getGroupId(int groupPosition) {
return groupPosition;
}