如何删除Android ExpandableListView中某个group item的child item?
澳门新葡京
澳门新葡京
当前位置 : 澳门新葡京 > 新葡京注册

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

自定义了一个expandablelistview 想要删除其中的子项

list使用remove方法把list中的指定项删除了

11-09 21:04:20.585: I/MainActivity13557: 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方法如下:

        forint i=0;i<3;i++{
            //给标题赋值
            Map<String,String> curGroupMap=new HashMap<String, String>;
            curGroupMap.put"group_title", groupStr[i];
            groupData.addcurGroupMap;
            
            //给子项赋值
            Map<String,String> curChildMap=new HashMap<String,String>;
            curChildMap.put"child_text", childStr[i];
            childData.addcurChildMap;
        } 

适配器代码如下:

    class MyAdapter extends BaseExpandableListAdapter{

        @Override
        public Object getChildint arg0, int arg1 {
            // TODO Auto-generated method stub
            return childData.getarg0;
        }

        @Override
        public long getChildIdint arg0, int arg1 {
            // TODO Auto-generated method stub
            return arg1;
        }

        @Override
        public View getChildViewint arg0, int arg1, boolean arg2, View arg3,
                ViewGroup arg4 {
            // TODO Auto-generated method stub
            
            ifarg3==null{
                LayoutInflater inflater=LayoutInflaterMain.
                        this.getSystemServiceContext.LAYOUT_INFLATER_SERVICE;
                arg3=inflater.inflateR.layout.child,null;
            }
            TextView textview=TextViewarg3.findViewByIdR.id.childtext;
            textview.setTextchildStr[arg0];
            
            return arg3;
        }

        @Override
        public int getChildrenCountint arg0 {
            // TODO Auto-generated method stub
            return childData.getarg0.size;
        }

        @Override
        public Object getGroupint arg0 {
            // TODO Auto-generated method stub
            return groupData.getarg0;
        }

        @Override
        public int getGroupCount {
            // TODO Auto-generated method stub
            return groupData.size;
        }

        @Override
        public long getGroupIdint arg0 {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getGroupViewint arg0, boolean arg1, View arg2,
                ViewGroup arg3 {
            // TODO Auto-generated method stub
            
            ifarg2==null{
                LayoutInflater inflater=LayoutInflaterMain.
                        this.getSystemServiceContext.LAYOUT_INFLATER_SERVICE;
                arg2=inflater.inflateR.layout.group, null;
            }
            TextView textview=TextViewarg2.findViewByIdR.id.grouptext;
            textview.setTextgroupStr[arg0];
            return arg2;
        }

        @Override
        public boolean hasStableIds {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public boolean isChildSelectableint arg0, int arg1 {
            // TODO Auto-generated method stub
            return true;
        }
        
    }

检查你的数据定义,这种做法是非常危险的。我们要确保每个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 getChildrenCountint arg0 {
    // 这里的arg0,代表groupPosition,
    // 所以,如果你删除了某个child元素,必然导致你的childData和groupData数量不对等。
    // 当adapter试图为group2调用这个方法时,崩溃。
    return childData.getarg0.size;
}

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

那么,正确的数据定义和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 createChildListsint 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.getgroupPosition;
childList.removechildPosition

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

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

好好好# title

栏目列表

广告位

澳门新葡京