I am trying to configure the navigation drawer to display 3 different types of "rows", but I'm having trouble with Holder
, being informed that message:
FATAL EXCEPTION: main java.lang.ClassCastException: $ com.coderockr.socialbase.row.ImageRow ViewHolder can not be cast to com.coderockr.socialbase.row.DescriptionRow $ ViewHolder
The code is here:
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;
}
}
}
What's the problem?
You forgot to tell the Adapter
how many different types you are supporting:
@Override
public int getViewTypeCount() {
return 3;
}
FIXED!
I found I was missing override two methods:
@Override
public int getViewTypeCount() {
return RowType.values().length;
}
@Override
public int getItemViewType(int position) {
return rows.get(position).getViewType();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.