google native ads RecyclerViewAdapter Object cannot be cast to NativeExpressAdView

689 views Asked by At

I had a working RecyclerView, but then I decided to add google native ads!

So I did change the RecyclerViewAdapter like google developers guideline!

It says that the List has to be a List of Object (List<Object>) So I changed this and a few other things in the Adapter!

To overgive my "List<DataForTextRow>" I just casted the whole List as Object!

customRVAdapterIndicator = new CustomRVAdapterCompareCountriesByIndicator(this.getActivity(), new ArrayList<Object>(listAdapter));

Then Android Studio shows no Errors and I'm able to launch the Application. But when I'm going to open the Activity with the mentioned RecyclerView it crashes.

It says:

DataForTextRow cannot be cast to com.google.android.gms.ads.NativeExpressAdView

This is the following line i onBindViewHolder

NativeExpressAdView adView = (NativeExpressAdView)dataForTextRowList.get(position);

So for testing I just commented the whole case AD_VIEW_TYPE in onBindViewHolder

Then I got the Error:

NativeExpressAdViewHolder cannot be cast to ....adapterr.CustomRVAdapterCompareCountriesByIndicator$IndicatorViewHolder 

This is in the following line:

IndicatorViewHolder indicatoViewHolder = (IndicatorViewHolder)( holder);

Is there anyone with a similar issue?

Thanks in advance! .

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

    private  List<Object> dataForTextRowList;
    private static Context mContext;
    private static final int MENU_ITEM_VIEW_TYPE=0;
    private static final int AD_VIEW_TYPE=1;

    public CustomRVAdapterCompareCountriesByIndicator(Context mContext, List<Object> dataForTextRowList) {
        this.mContext=mContext;
        this.dataForTextRowList = dataForTextRowList;
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {

        switch (position){
            case AD_VIEW_TYPE:
                View nativeExpressLayoutView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.native_express_ad_container,parent,false);
                return new NativeExpressAdViewHolder(nativeExpressLayoutView);

            case MENU_ITEM_VIEW_TYPE:
            default:
                View rowViewDetail = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.each_list_item_compare_countries,parent,false);
                IndicatorViewHolder indicatorViewHolder=new IndicatorViewHolder(rowViewDetail);
                return indicatorViewHolder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final int pos=position;
        int viewType = getItemViewType(position);

        switch (viewType){
            case AD_VIEW_TYPE:

            NativeExpressAdViewHolder nativeExpressHolder= (NativeExpressAdViewHolder)holder;
            NativeExpressAdView adView = (NativeExpressAdView)dataForTextRowList.get(position);


                ViewGroup adCardView = (ViewGroup)nativeExpressHolder.itemView;
                adCardView.removeAllViews();

                if (adView.getParent() != null){
                    ((ViewGroup)adView.getParent()).removeView(adView);
                }

                adCardView.addView(adView);
                break;
            case MENU_ITEM_VIEW_TYPE:
                default:

                    DecimalFormat formatter = new DecimalFormat("#,###,###.####");

                    IndicatorViewHolder indicatoViewHolder = (IndicatorViewHolder)((RecyclerView.ViewHolder) holder);
                    DataForTextRow item = (DataForTextRow) dataForTextRowList.get(position);

                    indicatoViewHolder.name.setText(((DataForTextRow)dataForTextRowList.get(position)).getTitle());


                    if (!((DataForTextRow)dataForTextRowList.get(position)).getTitleValue().equals("n.a.") ){
                        try {
                            indicatoViewHolder.value.setText(formatter.format( Double.valueOf( ((DataForTextRow)dataForTextRowList.get(position)).getTitleValue()))   );
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                    }else{
                        indicatoViewHolder.value.setText(((DataForTextRow)dataForTextRowList.get(position)).getTitleValue());
                    }

                    indicatoViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent i = new Intent(mContext, CompareCountriesListActivity.class);
                            i.putExtra("indicatorId",((DataForTextRow)dataForTextRowList.get(pos)).getIndicatorId());
                            i.putExtra("description", ((DataForTextRow)dataForTextRowList.get(pos)).getTitleDescription());
                            i.putExtra("title",((DataForTextRow)dataForTextRowList.get(pos)).getTitle());
                            i.putExtra("sourceOrganization",((DataForTextRow)dataForTextRowList.get(pos)).getSourceOrganization());
                            i.putExtra("topicNr",((DataForTextRow)dataForTextRowList.get(pos)).getTopicNr());
                            mContext.startActivity(i);
                        }
                    });

                    indicatoViewHolder.infoBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent i = new Intent(mContext, DetailsDescriptionPop.class);

                            i.putExtra("description", ((DataForTextRow)dataForTextRowList.get(pos)).getTitleDescription());
                            i.putExtra("title",((DataForTextRow)dataForTextRowList.get(pos)).getTitle());
                            i.putExtra("sourceOrganization",((DataForTextRow)dataForTextRowList.get(pos)).getSourceOrganization());
                            mContext.startActivity(i);
                        }
                    });
        }
    }

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

    @Override
    public int getItemViewType(int position) {
        return (position%8==0) ? AD_VIEW_TYPE: MENU_ITEM_VIEW_TYPE;
    }

    public class IndicatorViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView value; 
        ImageButton infoBtn;

        public IndicatorViewHolder(View itemView) {
            super(itemView);

            name = (TextView)itemView.findViewById(R.id.name);
            infoBtn = (ImageButton) itemView.findViewById(R.id.infoBtn);

        }
    }

    public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder{
        NativeExpressAdViewHolder(View view){
            super(view);
        }
    }    
}
0

There are 0 answers