Android ExpandableListView IndexOutOfBoundsException 删除项目后

Android ExpandableListView IndexOutOfBoundsException after remove item

从 ExpandableListView 中删除项目后出现 IndexOutOfBoundsException。

我的错误:

java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308)

我的列表适配器:

@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;
}

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

你的适配器和数据大小肯定有问题。请分享完整的适配器 class 然后我可以准确地解决你的问题。

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

我找到了答案

@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;
}