自定义了一个expandablelistview 想要删除其中的子项
list使用remove方法把list中的指定项删除了
11-09 21:04:20.585: I/MainActivity(13557): childData=[{child_text=second}, {child_text=third}]
但是在expandablelistview界面显示中,并没有变化,并且还有list溢出报错
定义部分
private ExpandableListView expandablelistview;
private MyAdapter expandableAdapter;
private String [] groupStr={"第一组","第二组","第三组"};
private String [] childStr={"first","second","third"};
private List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
private List<Map<String, String>> childData = new ArrayList<Map<String, String>>();
initData()方法如下:
for(int i=0;i<3;i++){
//给标题赋值
Map<String,String> curGroupMap=new HashMap<String, String>();
curGroupMap.put("group_title", groupStr[i]);
groupData.add(curGroupMap);
//给子项赋值
Map<String,String> curChildMap=new HashMap<String,String>();
curChildMap.put("child_text", childStr[i]);
childData.add(curChildMap);
}
适配器代码如下:
class MyAdapter extends BaseExpandableListAdapter{
@Override
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return childData.get(arg0);
}
@Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return arg1;
}
@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
ViewGroup arg4) {
// TODO Auto-generated method stub
if(arg3==null){
LayoutInflater inflater=(LayoutInflater)Main.
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg3=inflater.inflate(R.layout.child,null);
}
TextView textview=(TextView)arg3.findViewById(R.id.childtext);
textview.setText(childStr[arg0]);
return arg3;
}
@Override
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return childData.get(arg0).size();
}
@Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return groupData.get(arg0);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groupData.size();
}
@Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getGroupView(int arg0, boolean arg1, View arg2,
ViewGroup arg3) {
// TODO Auto-generated method stub
if(arg2==null){
LayoutInflater inflater=(LayoutInflater)Main.
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg2=inflater.inflate(R.layout.group, null);
}
TextView textview=(TextView)arg2.findViewById(R.id.grouptext);
textview.setText(groupStr[arg0]);
return arg2;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return true;
}
}
检查你的数据定义,这种做法是非常危险的。我们要确保每个group item都有一个对应的child list,这个list为空,代表该group item没有子条目。
可是你的定义呢?如果删除了"second",那么“thrid”到底是group1,还是group2的child呢?从代码中细究:
那么,正确的数据定义和adapter数据初始化应该怎么做呢?看adapter的构造器:
(1) 应该像这样定义child data:
List<? extends List<? extends Map<String, ?>>>
(2) 然后这样删除child item:
(3) 再然后
notifyDataSetChanged
通知adapter刷新View。