Android 对组使用两个不同的视图持有者时ExpandableListView类CastException
当使用两个不同的Android 对组使用两个不同的视图持有者时ExpandableListView类CastException,android,expandablelistview,expandablelistadapter,Android,Expandablelistview,Expandablelistadapter,当使用两个不同的视图持有者时,我得到一个ClassCastException 对于我的组,请在可扩展列表视图中查看 我有一个单独的ViewHolder用于位置0的组。其余的组使用不同的组 此错误发生在我已通过向下滚动并向上滚动到位置0的组来创建视图之后 我知道我的问题在这方面: vPropertyInfo=(ViewHolderPropertyInfo)convertView.getTag() 当我滚动回组位置0时,它尝试将(ViewHolderPropertyInfo)强制转换为转换视图,该视
视图持有者时,我得到一个ClassCastException
对于我的组,请在可扩展列表视图中查看
我有一个单独的ViewHolder
用于位置0的组。其余的组使用不同的组
此错误发生在我已通过向下滚动并向上滚动到位置0的组来创建视图之后
我知道我的问题在这方面:
vPropertyInfo=(ViewHolderPropertyInfo)convertView.getTag()代码>
当我滚动回组位置0时,它尝试将(ViewHolderPropertyInfo)
强制转换为转换视图
,该视图不为空,但实际上是(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;
}
}
我找到了一种更优雅的方法来解决问题。它是在检查convertView==null
之前执行convertView.getTag()
检查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;
}
}