我正在尝试配置导航抽屉以显示3种不同类型的“行”,但我遇到Holder
时遇到问题,被告知该消息:
FATAL EXCEPTION:主java.lang.ClassCastException:$ com.coderockr.socialbase.row.ImageRow ViewHolder无法强制转换为 com.coderockr.socialbase.row.DescriptionRow $ ViewHolder
代码在这里:
public class MenuAdapter extends BaseAdapter {
private final Context mContext;
private final ArrayList<MenuItemModel> mItems;
private final int mCount;
private final List<Row> rows;
public MenuAdapter(Context context, ArrayList<MenuItemModel> items) {
mContext = context;
mItems = items;
mCount = mItems.size();
rows = new ArrayList<Row>();
for(MenuItemModel model : mItems) {
if((model.ismItemType()) && (model.getCounter() == null)) {
rows.add(new BarRow(LayoutInflater.from(mContext), model));
} else if(model.getImageId() != null) {
rows.add(new ImageRow(LayoutInflater.from(mContext), model));
} else {
rows.add(new DescriptionRow(LayoutInflater.from(mContext), model));
}
}
}
@Override
public int getCount() {
return mCount;
}
@Override
public MenuItemModel getItem(int position) { return mItems.get(position); }
@Override
public long getItemId(int position) { return getItem(position).getPosition(); }
@Override
public View getView(int position, View convertView, ViewGroup parent) { return rows.get(position).getView(convertView); }
DescriptionRow:
public class DescriptionRow implements Row {
private final MenuItemModel item;
private LayoutInflater inflater;
private int mCounter;
public DescriptionRow(LayoutInflater inflater, MenuItemModel item) {
this.item = item;
this.inflater = inflater;
}
@Override
public View getView(View convertView) {
ViewHolder holder;
View view;
mCounter = Integer.parseInt(item.getCounter());
if(convertView == null) {
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.text_row, null);
holder = new ViewHolder((TextView)viewGroup.findViewById(R.id.title),
(TextView)viewGroup.findViewById(R.id.description),
(RelativeLayout)viewGroup.findViewById(R.id.relativeLayout_circle));
viewGroup.setTag(holder);
view = viewGroup;
} else {
view = convertView;
holder = (ViewHolder)convertView.getTag();
}
holder.titleView.setText(item.getitemName());
holder.descriptionView.setText(item.getitemName());
if(mCounter < 1) {
holder.relativeLayout_Cicle.setVisibility(View.INVISIBLE);
}
return view;
}
@Override
public int getViewType() { return RowType.DESCRIPTION_ROW.ordinal(); }
public static class ViewHolder {
final TextView titleView;
final TextView descriptionView;
final RelativeLayout relativeLayout_Cicle;
private ViewHolder(TextView titleView, TextView descriptionView, RelativeLayout relativeCicle) {
this.titleView = titleView;
this.descriptionView = descriptionView;
this.relativeLayout_Cicle = relativeCicle;
}
}
}
有什么问题?
答案 0 :(得分:1)
您忘了告诉Adapter
您支持多少种不同的类型:
@Override
public int getViewTypeCount() {
return 3;
}
答案 1 :(得分:1)
固定!
我发现我遗漏了两种方法:
@Override
public int getViewTypeCount() {
return RowType.values().length;
}
@Override
public int getItemViewType(int position) {
return rows.get(position).getViewType();
}