我正在尝试开发我的第一个 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;
}
}
不负相思意
相关分类