javaandroidsectionedrecyclerviewadapter

java.lang.ClassCastException android


I need to do two recyclerViews with sections in my android app. The first one works well, but the second one shows such error

 java.lang.ClassCastException: com.example.battlefield.WaysRecyclerViewAdapter.WaysItemViewHolder cannot be cast to com.example.battlefield.WaysRecyclerViewAdapter.WaysHeaderViewHolder
                                                                         at com.example.battlefield.WaysRecyclerViewAdapter.WaysRecyclerViewAdapter.onBindHeaderViewHolder(WaysRecyclerViewAdapter.java:45)

I separated viewHolders for sectionHeaders and items of recyclerView. There are my classes WaysItemViewHolder:

public class WaysItemViewHolder extends RecyclerView.ViewHolder{
    public TextView waysName;
    public TextView waysInfo;
    public TextView waysTags;
    public Button addWay;
    public WaysItemViewHolder(View itemView) {
        super(itemView);
        waysName = (TextView)itemView.findViewById(R.id.cv_ways_name);
        waysInfo = (TextView)itemView.findViewById(R.id.cv_ways_info);
        waysTags = (TextView)itemView.findViewById(R.id.cv_ways_tags);
        addWay = (Button) itemView.findViewById(R.id.cv_ways_add_way);
}}

WaysHeaderViewHolder:

public class WaysHeaderViewHolder extends RecyclerView.ViewHolder{
    public TextView headerTitle;
    public WaysHeaderViewHolder(View itemView) {
        super(itemView);
        headerTitle = (TextView)itemView.findViewById(R.id.cv_ways_header);
    }
}

And recyclerViewAdapter:

    public class WaysRecyclerViewAdapter extends StatelessSection {
        private static final String TAG = WaysRecyclerViewAdapter.class.getSimpleName();
        private String title;
        private String[] waysList;
        private String[] infoList;
    private String[] tagsList;
    public WaysRecyclerViewAdapter(String title, String[] waysList, String[] tagsList, String[] infoList) {
        super(R.layout.cardview_way_header, R.layout.cardview_way_item);
        this.title = title;
        this.waysList = waysList;
        this.tagsList = tagsList;
        this.infoList = infoList;
    }
    @Override
    public int getContentItemsTotal() {
        return waysList.length;
    }
    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new WaysItemViewHolder(view);
    }
    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        WaysItemViewHolder iHolder = (WaysItemViewHolder)holder;
        iHolder.waysName.setText(waysList[position]);
        iHolder.waysInfo.setText(infoList[position]);
        iHolder.waysTags.setText(tagsList[position]);
    }
    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new WaysItemViewHolder(view);
    }
    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        WaysHeaderViewHolder hHolder = (WaysHeaderViewHolder)holder;
        hHolder.headerTitle.setText(title);
    }
}

For making sections in recyclerView, i used this library https://github.com/luizgrp/SectionedRecyclerViewAdapter

Anybody know how to resolve it?


Solution

  • @Override public RecyclerView.ViewHolder getHeaderViewHolder(View view) { return new WaysItemViewHolder(view); }

    You need to replace WaysItemViewHolder with WaysHeaderViewHolder