Expanpandable ListView在删除最后一个子组时发生崩溃

时间:2014-03-05 14:09:31

标签: android listview android-listview expandablelistview

当我从可扩展列表视图中的组中删除项目中的子项时,我遇到了问题。问题是我的组扩展后我删除了当我调用子“getChildView”时 在我的ListView适配器上执行“notifyDataSetChanged”。我正在访问一个大小现在为零的数组,并获得Out Of Bounds异常。我假设Expandable Listview会自动删除视图,因为“getChildrenCount”返回0.如果该组折叠,则不会出现此问题。列表从不显示子视图,因为没有更多子项。

我的问题是如何删除可扩展ListView中扩展组的最后一个子项

以下是我的代码示例。了解条件是该组已扩展并且我已删除所有子项,并且此函数仍在被调用。

    // This Function used to inflate child rows view
    @Override
    public View getChildView(
        int groupPosition,
        int childPosition,
        boolean isLastChild, 
        View convertView,
        ViewGroup parentView)
    {
        Trade t = objArray[groupPosition];

        View childView = convertView;
        if( childView == null )
        {
            LayoutInflater inflater = LayoutInflater.from(client);
            childView = inflater.inflate(R.layout.item_textview, parentView, false);
            ChildViewHolder childViewHolder = new ChildViewHolder();

            childViewHolder.itemTextView = (TextView)childView.findViewById(R.id.itemTextView);
            childView.setTag(childViewHolder);
        }

        ChildViewHolder childViewHolder = (ChildViewHolder)childView.getTag();

        childViewHolder.itemTextView.setText("Term: " + t.getTradeTerms().get(childPosition));
        return childView;
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {
        return objArray[groupPosition].getTradeTerms().size();
    }

1 个答案:

答案 0 :(得分:0)

请看下面的代码我希望这是非常有用的。

HashMap<String, List<String>> listDataChild;
 List<String> listDataHeader;

// Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub

          listDataChild.get(listDataHeader.get(groupPosition)).remove(childPosition);
           adapter.notifyDataSetChanged();

            return false;
        }
    });