I am getting a ClassCastException when using two different ViewHolders
for my Groups an in ExpandableListView.
I have a separate ViewHolder for Group in position 0. The rest of the Groups use a different one.
This error happens after I already create the views by scrolling down and scroll back up to the Group in position 0.
I know my issue is in this line:
vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();
When I scroll back to Group position 0, it tries to cast (ViewHolderPropertyInfo) to the convertView that is not null, but it is actually (ViewHolderGroup) type. How do I resolve this?
@Override
public View getGroupView(int groupPosition, boolean isExpandable, View convertView, ViewGroup parent) {
    if (groupPosition == 0) {
        ViewHolderPropertyInfo vhPropertyInfo;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row_property_info, null);
            vhPropertyInfo = new ViewHolderPropertyInfo();
            vhPropertyInfo.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
            vhPropertyInfo.mTvCountry = (TextView) convertView.findViewById(R.id.tv_country);
            vhPropertyInfo.mTvCity = (TextView) convertView.findViewById(R.id.tv_city);
            vhPropertyInfo.mTvDistrict = (TextView) convertView.findViewById(R.id.tv_district);
            vhPropertyInfo.mTvPostalCode = (TextView) convertView.findViewById(R.id.tv_postal_code);
            vhPropertyInfo.mTvStreet = (TextView) convertView.findViewById(R.id.tv_street);
            vhPropertyInfo.mTvSubStreet = (TextView) convertView.findViewById(R.id.tv_sub_street);
            convertView.setTag(vhPropertyInfo);
        }
        else {
            vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();
        }
        vhPropertyInfo.mTvName.setText("House Lannister");
        vhPropertyInfo.mTvCountry.setText("Westeros");
        vhPropertyInfo.mTvCity.setText("Kings Landing");
        vhPropertyInfo.mTvDistrict.setText("West Side");
        vhPropertyInfo.mTvPostalCode.setText("12345");
        vhPropertyInfo.mTvStreet.setText("123 Kings Lane");
        vhPropertyInfo.mTvSubStreet.setText("Hut 23");
        return convertView;
    }
    else {
        ViewHolderGroup vhGroup;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row_property_group, null);
            vhGroup = new ViewHolderGroup();
            vhGroup.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(vhGroup);
        }
        else {
            vhGroup = (ViewHolderGroup) convertView.getTag();
        }
        // Group 0 is property info so have to subtract one position
        vhGroup.mTvName.setText(getGroup(groupPosition));
        return convertView;
    }
}
public final class ViewHolderGroup {
    public TextView mTvName;
}
public final class ViewHolderPropertyInfo {
    public TextView mTvName;
    public TextView mTvCountry;
    public TextView mTvCity;
    public TextView mTvDistrict;
    public TextView mTvPostalCode;
    public TextView mTvStreet;
    public TextView mTvSubStreet;
}
EDIT 1: So I fixed the problem, but checking the instance of convertView.getTag() if it was the same type of ViewHolder type, but my solution doesn't seem very elegant. Anyone know a way to do this cleaner?
@Override
public View getGroupView(int groupPosition, boolean isExpandable, View convertView, ViewGroup parent) {
    if (groupPosition == 0) {
        ViewHolderPropertyInfo vhPropertyInfo;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row_property_info, null);
            vhPropertyInfo = new ViewHolderPropertyInfo();
            vhPropertyInfo.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
            vhPropertyInfo.mTvCountry = (TextView) convertView.findViewById(R.id.tv_country);
            vhPropertyInfo.mTvCity = (TextView) convertView.findViewById(R.id.tv_city);
            vhPropertyInfo.mTvDistrict = (TextView) convertView.findViewById(R.id.tv_district);
            vhPropertyInfo.mTvPostalCode = (TextView) convertView.findViewById(R.id.tv_postal_code);
            vhPropertyInfo.mTvStreet = (TextView) convertView.findViewById(R.id.tv_street);
            vhPropertyInfo.mTvSubStreet = (TextView) convertView.findViewById(R.id.tv_sub_street);
            convertView.setTag(vhPropertyInfo);
        }
        else {
            if (convertView.getTag() instanceof ViewHolderPropertyInfo) {
                vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();
            }
            else {
                convertView = mInflater.inflate(R.layout.row_property_info, null);
                vhPropertyInfo = new ViewHolderPropertyInfo();
                vhPropertyInfo.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
                vhPropertyInfo.mTvCountry = (TextView) convertView.findViewById(R.id.tv_country);
                vhPropertyInfo.mTvCity = (TextView) convertView.findViewById(R.id.tv_city);
                vhPropertyInfo.mTvDistrict = (TextView) convertView.findViewById(R.id.tv_district);
                vhPropertyInfo.mTvPostalCode = (TextView) convertView.findViewById(R.id.tv_postal_code);
                vhPropertyInfo.mTvStreet = (TextView) convertView.findViewById(R.id.tv_street);
                vhPropertyInfo.mTvSubStreet = (TextView) convertView.findViewById(R.id.tv_sub_street);
                convertView.setTag(vhPropertyInfo);
            }
        }
        vhPropertyInfo.mTvName.setText("House Lannister");
        vhPropertyInfo.mTvCountry.setText("Westeros");
        vhPropertyInfo.mTvCity.setText("Kings Landing");
        vhPropertyInfo.mTvDistrict.setText("West Side");
        vhPropertyInfo.mTvPostalCode.setText("12345");
        vhPropertyInfo.mTvStreet.setText("123 Kings Lane");
        vhPropertyInfo.mTvSubStreet.setText("Hut 23");
        return convertView;
    }
    else {
        ViewHolderGroup vhGroup;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row_property_group, null);
            vhGroup = new ViewHolderGroup();
            vhGroup.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(vhGroup);
        }
        else {
            if (convertView.getTag() instanceof ViewHolderGroup) {
                vhGroup = (ViewHolderGroup) convertView.getTag();
            }
            else {
                convertView = mInflater.inflate(R.layout.row_property_group, null);
                vhGroup = new ViewHolderGroup();
                vhGroup.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
                convertView.setTag(vhGroup);
            }
        }
        // Group 0 is property info so have to subtract one position
        vhGroup.mTvName.setText(getGroup(groupPosition));
        return convertView;
    }
}
				
                        
I found a more elegant way to do my solution. It is to do the
convertView.getTag()checking the instanceof the ViewHolder before the check ifconvertView == null.