Casting Problem in RecyclerViewAdapter with Multiple View Holders

608 views Asked by At

Hey everyone ım trying to add Headers and Items to my recyclerview. But after trying to open the page, its giving me a casting error. I checked my code but ı couldn't find an error for this if you wanna check this is the code

public class NewRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;

Context mContext;
List<ListItem> listItems;

public NewRecyclerViewAdapter(Context context, List<ListItem> listItems) {
    mContext = context;
    this.listItems = listItems;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    if(viewType == TYPE_HEADER) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_layout, parent, false);
        return  new VHHeader(v);

    }else if(viewType == TYPE_ITEM) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_item, parent, false);
        return new VHItem(v);

    }
    throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}



@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
    if(holder instanceof VHHeader)
    {
        HeaderTest currentItem = (HeaderTest) listItems.get(position);
        VHHeader vhHeader = (VHHeader) holder;
        vhHeader.headerText.setText(currentItem.getName());
        vhHeader.headerImage.setImageResource(currentItem.getImage());
    }
    else if(holder instanceof VHItem)
    {
        PosterTest currentItem = (PosterTest) listItems.get(position);
        VHItem vhItem = (VHItem) holder;
        vhItem.textPosterTitle.setText(currentItem.getName());
        vhItem.posterImageView.setImageResource(currentItem.getImage());
    }
}

//    need to override this method
@Override
public int getItemViewType(int position) {
    if(isPositionHeader(position))
        return TYPE_HEADER;
    return TYPE_ITEM;
}

private boolean isPositionHeader(int position)
{
    return position == 0;
}

@Override
public int getItemCount() {
    return listItems.size();
}



class VHItem extends RecyclerView.ViewHolder {

    private TextView textPosterTitle;
    private ImageView posterImageView;
    private CardView cardView;


    public VHItem(@NonNull View itemView) {
        super(itemView);
        textPosterTitle = (TextView) itemView.findViewById(R.id.poster_title_id);
        posterImageView = (ImageView) itemView.findViewById(R.id.poster_image_id);
        cardView = (CardView) itemView.findViewById(R.id.cardview_id);
    }
}

class VHHeader extends RecyclerView.ViewHolder{

    private TextView headerText;
    private ImageView headerImage;

    public VHHeader(@NonNull View itemView) {
        super(itemView);
        headerText = (TextView) itemView.findViewById(R.id.headerText);
        headerImage = (ImageView) itemView.findViewById(R.id.headerImage);
    }
    
    
    
}

}

public class ProductsFragment extends Fragment {

private RecyclerView mRecyclerView;
public List<ListItem> mData;
NewRecyclerViewAdapter adapter;

public ProductsFragment() {

}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_products, container, false);

    mRecyclerView = view.findViewById(R.id.recyclerView);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    getListData();
    adapter = new NewRecyclerViewAdapter(getContext(),mData);
    mRecyclerView.setAdapter(adapter);

    
    return view;
}

private void getListData() {
    mData = DataList.getTimeLineData();
}

}

public class DataList{ public static List getTimeLineData(){

    List<ListItem> combinedList = new ArrayList<>();

    List<ListItem> movieList = new ArrayList<>();

    HeaderTest itemHeaderMovies = new HeaderTest("Movies", R.drawable.dof_logo,1);
    PosterTest itemPosterMovies = new PosterTest("poster1",R.drawable.aerobotics,1);
    PosterTest itemPosterMoviesTwo = new PosterTest("poster2",R.drawable.aerobotics,1);

    movieList.add(itemHeaderMovies);
    movieList.add(itemPosterMovies);
    movieList.add(itemPosterMoviesTwo);

    List<ListItem> productList = new ArrayList<>();

    HeaderTest itemHeaderProduct = new HeaderTest("Product",R.drawable.dof_logo,1);
    PosterTest itemPosterProduct = new PosterTest("poster3",R.drawable.aerobotics,1);
    PosterTest itemPosterProductTwo = new PosterTest("poster4",R.drawable.aerobotics,1);

    movieList.add(itemHeaderProduct);
    movieList.add(itemPosterProduct);
    movieList.add(itemPosterProductTwo);

    combinedList.addAll(movieList);
    combinedList.addAll(productList);

    return combinedList;

}

}

And this is the error

1

There are 1 answers

0
Onur Serbes On

I fixed the errors, the problem was in the getItemViewType part. This is how I changed my code. and I deleted the isPositionHeader method, they were unnecessary

public int getItemViewType(int position) {
    if(listItems.get(position).getId() == 1)
        return TYPE_HEADER;
    return TYPE_ITEM;
}

ı add the "id" variable to the objects that ı wanted to use as view and ı wrote the function with using it.