使用两个不同的ViewHolders for Group时,ExpandableListView ClassCastException

时间:2015-06-12 22:02:18

标签: android expandablelistview expandablelistadapter

我在使用两个不同的ClassCastException时收到ViewHolders 我的论坛中的ExpandableListView

我在第0位有一个单独的ViewHolder组。其余的组使用不同的组。

在我通过向下滚动并向上滚动回到位置0中的组来创建视图后,会发生此错误。

我知道我的问题在于这一行: 的 vhPropertyInfo = (ViewHolderPropertyInfo) convertView.getTag();

当我回滚到组位置0时,它会尝试将(ViewHolderPropertyInfo)强制转换为非空的convertView,但它实际上是(ViewHolderGroup)类型。我该如何解决这个问题?

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

编辑1:所以我修复了问题,但检查convertView.getTag()的实例是否是ViewHolder类型的相同类型,但我的解决方案看起来并不优雅。有人知道如何做到更清洁吗?

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

1 个答案:

答案 0 :(得分:0)

我找到了一种更优雅的方式来解决问题。在检查convertView.getTag()之前,是convertView == null检查ViewHolder的实例。

@Override
public View getGroupView(int groupPosition, boolean isExpandable, View convertView, ViewGroup parent) {
    if (groupPosition == 0) {
        ViewHolderPropertyInfo vhPropertyInfo;

        if (convertView != null && !(convertView.getTag() instanceof ViewHolderPropertyInfo)) {
            convertView = null;
        }

        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.getTag() instanceof ViewHolderGroup)) {
            convertView = null;
        }

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