Help with failure in an adapter of a recyclerview, when putting several items

2

and thanks in advance. I show you my code to see if you can tell me what my mistake is ...

public class AdapterHome extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    private ArrayList<Item> items;
    Context context;

    public class ViewHolder1 extends RecyclerView.ViewHolder
    {
        View view;

        public TextView invoquer;
        public TextView name;
        public TextView text;
        public ImageView icon;
        public ImageView champ1;
        public ImageView champ2;
        public ImageView champ3;
        public ImageView champ4;
        public ImageView champ5;



        public ViewHolder1(View v)
        {
            super(v);
            this.view = v;
            invoquer = (TextView) v.findViewById(R.id.invoquer);
            name = (TextView) v.findViewById(R.id.name);
            text = (TextView) v.findViewById(R.id.text);
            icon = (ImageView) v.findViewById(R.id.icon);
            champ1 = (ImageView) v.findViewById(R.id.champ1);
            champ2 = (ImageView) v.findViewById(R.id.champ2);
            champ3 = (ImageView) v.findViewById(R.id.champ3);
            champ4 = (ImageView) v.findViewById(R.id.champ4);
            champ5 = (ImageView) v.findViewById(R.id.champ5);

        }
    }

    public class ViewHolder2 extends RecyclerView.ViewHolder
    {
        View view;

        public TextView invoquer;
        public TextView name;
        public TextView text;
        public ImageView icon;
        public ImageView champ1;
        public ImageView champ2;
        public ImageView champ3;
        public ImageView champ4;



        public ViewHolder2(View v)
        {
            super(v);
            this.view = v;
            invoquer = (TextView) v.findViewById(R.id.invoquer);
            name = (TextView) v.findViewById(R.id.name);
            text = (TextView) v.findViewById(R.id.text);
            icon = (ImageView) v.findViewById(R.id.icon);
            champ1 = (ImageView) v.findViewById(R.id.champ1);
            champ2 = (ImageView) v.findViewById(R.id.champ2);
            champ3 = (ImageView) v.findViewById(R.id.champ3);
            champ4 = (ImageView) v.findViewById(R.id.champ4);

        }
    }

    @Override
    public int getItemViewType(int position)
    {
        Log.e("LOLSTRATEGIIIII", "*****************************************************************  "+items.get(position).getItemType());
        return items.get(position).getItemType();
    }

    public AdapterHome(ArrayList<Item> items)
    {
        this.items = items;
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v;
        RecyclerView.ViewHolder viewHolder;

        switch(viewType)
        {
            case 1:
                v = LayoutInflater.from(parent.getContext()).inflate(R.layout.strategy_item,parent,false);
                context = v.getContext();
                viewHolder =  new ViewHolder1(v);
                break;
            case 2:
                v = LayoutInflater.from(parent.getContext()).inflate(R.layout.combo_item,parent,false);
                context = v.getContext();
                viewHolder =  new ViewHolder2(v);
                break;
            default:
                return null;
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        String url = "http://ddragon.leagueoflegends.com/cdn/img/champion/loading/";
        String imgUrl = "http://ddragon.leagueoflegends.com/cdn/8.1.1/img/profileicon/1145.png";
        String urlCham = "https://ddragon.leagueoflegends.com/cdn/7.10.1/img/champion/";

        Log.e("LOLSTRATEGIIIII", "*****************************************************************  "+holder.getItemViewType());

        switch(holder.getItemViewType())
        {
            case 1:
                Picasso.with(context).load(url+items.get(position).getChampions().get(0)).into(((ViewHolder1)holder).champ1);
                Picasso.with(context).load(url+items.get(position).getChampions().get(1)).into(((ViewHolder1)holder).champ2);
                Picasso.with(context).load(url+items.get(position).getChampions().get(2)).into(((ViewHolder1)holder).champ3);
                Picasso.with(context).load(url+items.get(position).getChampions().get(3)).into(((ViewHolder1)holder).champ4);
                Picasso.with(context).load(url+items.get(position).getChampions().get(4)).into(((ViewHolder1)holder).champ5);
                Picasso.with(context).load(imgUrl).into(((ViewHolder1)holder).icon);
                ((ViewHolder1)holder).name.setText(items.get(position).getName());
                ((ViewHolder1)holder).text.setText(items.get(position).getText());
                ((ViewHolder1)holder).invoquer.setText(items.get(position).getInvoquer());
            case 2:
                Picasso.with(context).load(urlCham+items.get(position).getChampions().get(0)).into(((ViewHolder2)holder).champ1);
                Picasso.with(context).load(urlCham+items.get(position).getChampions().get(1)).into(((ViewHolder2)holder).champ2);
                Picasso.with(context).load(urlCham+items.get(position).getChampions().get(2)).into(((ViewHolder2)holder).champ3);
                Picasso.with(context).load(urlCham+items.get(position).getChampions().get(3)).into(((ViewHolder2)holder).champ4);
                Picasso.with(context).load(imgUrl).into(((ViewHolder2)holder).icon);
                ((ViewHolder2)holder).name.setText(items.get(position).getName());
                ((ViewHolder2)holder).text.setText(items.get(position).getText());
                ((ViewHolder2)holder).invoquer.setText(items.get(position).getInvoquer());
        }



    }

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

The error it gives me is that I can not cast ViewHolder1 to ViewHolder2, and I try to do it from the generic RecyclerView.ViewHolder to ViewHolder2 (This happens in the onBindViewHolder ()).

The thing is precisely that I can not understand why it has a holder reference to ViewHolder1 if I'm telling it to just try to cast ViewHolder2 when the item type is 2 ...

    
asked by Adrián Palma Lima 27.04.2018 в 01:24
source

1 answer

0

ViewHolder1 and ViewHolder2 are not related to each other so you can not cast. What you can do is cast both ViewHolders to ViewHolder but not between them.

The most likely thing is that when you trap the object holder to ViewHolder2 , in reality holder contains a reference of ViewHolder1 and hence the error. You have to confirm that it is of type ViewHolder2 to be able to affect the casting.

    
answered by 27.04.2018 в 03:59