如何删除Android ExpandableListView中某个group item的child item?

程序员张晴天
  • 224

自定义了一个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;
        }
        
    }
回复
阅读 5.7k
3 个回答
li21
  • 3.1k
✓ 已被采纳

检查你的数据定义,这种做法是非常危险的。我们要确保每个group item都有一个对应的child list,这个list为空,代表该group item没有子条目。
可是你的定义呢?如果删除了"second",那么“thrid”到底是group1,还是group2的child呢?从代码中细究:

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>>();

@Override
public int getChildrenCount(int arg0) {
    // 这里的arg0,代表groupPosition,
    // 所以,如果你删除了某个child元素,必然导致你的childData和groupData数量不对等。
    // 当adapter试图为group2调用这个方法时,崩溃。
    return childData.get(arg0).size();
}

@Override
public Object getChild(int arg0, int arg1) {
    return childData.get(arg0);
    
    // arg0代表groupPosition,arg1代表childPosition,
    // 建议你把这两个参数名称从arg*改成有意义的名字。
    // 修改了childData的定义后,这里需要改写成:
    return childData.get(arg0).get(arg1);
}

那么,正确的数据定义和adapter数据初始化应该怎么做呢?看adapter的构造器:

public SimpleExpandableListAdapter(
    List<? extends Map<String, ?>> groupData, 
    List<? extends List<? extends Map<String, ?>>> childData) {}

(1) 应该像这样定义child data:List<? extends List<? extends Map<String, ?>>>

private List createChildLists(int groupCount) {
    ArrayList result = new ArrayList();
    for( int i = 0 ; i < groupCount ; i++ ) {
      ArrayList secList = new ArrayList(); 
      for( int n = 0 ; n < 3 ; n++ ) {
        HashMap child = new HashMap();
        child.put( "Sub Item", "Sub Item " + n );            
        secList.add( child );
      }
     result.add( secList );
    }         
    return result;
}

(2) 然后这样删除child item:

List<Map<String, String>> childList = (List<Map<String, String>>)childData.get(groupPosition);
childList.remove(childPosition)

(3) 再然后 notifyDataSetChanged 通知adapter刷新View。

删除数组里对应的值,然后调用MyAdapter.notifyDataSetChanged()

1陈二菜
  • 1
新手上路,请多包涵

好好好# title

宣传栏