Search code examples
androidexpandablelistviewindexoutofboundsexceptionremovechild

Android ExpandableListView IndexOutOfBoundsException after remove item


Got IndexOutOfBoundsException after delete item from ExpandableListView.

My errors:

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)

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

Any help will be highly appreciated. Thank you.


Solution

  • I found my answer

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