删除项目后,Android ExpandableListView IndexOutOfBoundsException

时间:2018-07-16 03:54:53

标签: android expandablelistview indexoutofboundsexception removechild

从ExpandableListView删除项目后出现IndexOutOfBoundsException。

我的错误:

  

java.lang.IndexOutOfBoundsException:无效的索引3,大小为3           在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)           在java.util.ArrayList.get(ArrayList.java:308)

我的ListAdapter:

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    BookmarkParentModel bookmarkParent = (BookmarkParentModel) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.header_bookmark, null);
    }

    TextView bookmarkHeader = (TextView) convertView.findViewById(R.id.bookmark_header);
    TextView bookmarkCount = (TextView) convertView.findViewById(R.id.bookmark_count);
    bookmarkHeader.setText(bookmarkParent.getString1().trim());
    bookmarkCount.setText(bookmarkParent.getString2().trim());
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    BookmarkChildModel bookmarkChild = (BookmarkChildModel) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_bookmark, null);
    }
    TextView letterName = (TextView) convertView.findViewById(R.id.letter_name);
    TextView bookmarkDate = (TextView) convertView.findViewById(R.id.bookmark_date);
    letterName.setText(bookmarkChild.getString1().trim());
    bookmarkDate.setText(bookmarkChild.getString2().trim());

    return convertView;
}

任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:0)

我找到了答案

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.header_bookmark, null);
    }

    if (groupPosition < getGroupCount()) {
        BookmarkParentModel bookmarkParent = (BookmarkParentModel) getGroup(groupPosition);
        TextView bookmarkHeader = (TextView) convertView.findViewById(R.id.bookmark_header);
        TextView bookmarkCount = (TextView) convertView.findViewById(R.id.bookmark_count);
        bookmarkHeader.setText(bookmarkParent.getString1().trim());
        bookmarkCount.setText(bookmarkParent.getString2().trim());
    }

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_bookmark, null);
    }

    if (childPosition < getChildrenCount(groupPosition)) {
        BookmarkChildModel bookmarkChild = (BookmarkChildModel) getChild(groupPosition, childPosition);
        TextView letterName = (TextView) convertView.findViewById(R.id.letter_name);
        TextView bookmarkDate = (TextView) convertView.findViewById(R.id.bookmark_date);
        letterName.setText(bookmarkChild.getString1().trim());
        bookmarkDate.setText(bookmarkChild.getString2().trim());
    }
    return convertView;
}

答案 1 :(得分:-1)

适配器和数据大小一定有问题。请共享完整的适配器类,然后我就可以准确解决您的问题。

检查删除数据后是否要更新大小?