viewholder和导航抽屉的问题

7gyucuyw  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(261)

我试图配置导航抽屉显示3种不同类型的“行”,但我有麻烦的 Holder ,被告知:
致命异常:主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); }

说明行:

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

}
有什么问题吗?

ou6hu8tu

ou6hu8tu1#

你忘了告诉警察 Adapter 您支持多少种不同的类型:

@Override
public int getViewTypeCount() {
    return 3;
}
pkmbmrz7

pkmbmrz72#

固定的!
我发现我缺少两种方法:

@Override
public int getViewTypeCount() {
    return RowType.values().length;
}

@Override
public int getItemViewType(int position) {
    return rows.get(position).getViewType();
}

相关问题