gpt4 book ai didi

java - viewHolder 和抽屉导航的问题

转载 作者:行者123 更新时间:2023-11-29 21:12:39 31 4
gpt4 key购买 nike

我正在尝试配置抽屉导航以显示 3 种不同类型的“行”,但我在使用 Holder 时遇到了问题,被告知该消息:

FATAL EXCEPTION: main java.lang.ClassCastException: $ com.coderockr.socialbase.row.ImageRow ViewHolder can not be cast to 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;
}
}

有什么问题?

最佳答案

您忘记告诉适配器您支持多少种不同的类型:

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

关于java - viewHolder 和抽屉导航的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22329122/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com