我正在尝试开发我的第一个 android 应用程序,但遇到以下错误 - 'java.lang.ClassCastException。这是我的java代码:package com.example.fresh24;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import androidx.annotation.NonNull;import androidx.recyclerview.widget.RecyclerView;import java.util.List;public class CartAdapter extends RecyclerView.Adapter { private List<CartItemModel> cartItemModelList; public CartAdapter(List<CartItemModel> cartItemModelList) { this.cartItemModelList = cartItemModelList; } @Override public int getItemViewType(int position) { switch (cartItemModelList.get(position).getType()) { case 0: return CartItemModel.CART_ITEM; case 1: return CartItemModel.TOTAL_AMOUNT; default: return -1; } } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) { switch (viewType) { case CartItemModel.CART_ITEM: View cartItemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cart_item_layout, viewGroup, false); return new CartItemViewHolder(cartItemView); case CartItemModel.TOTAL_AMOUNT: View cartTotalView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cart_total_amount_layout, viewGroup, false); return new CartItemViewHolder(cartTotalView); default: return null; } }
1 回答
不负相思意
TA贡献1777条经验 获得超10个赞
问题在这里
View cartTotalView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cart_total_amount_layout, viewGroup, false);
return new CartItemViewHolder(cartTotalView); // Returning wrong ViewHolder it should be CartTotalAmountViewHolder
添加回答
举报
0/150
提交
取消