Нативная реклама Google RecyclerViewAdapter Объект нельзя преобразовать в NativeExpressAdView

У меня была рабочая RecyclerView, но тогда я решил добавить нативную рекламу Google!

Так что я изменил RecyclerViewAdapter как руководство для разработчиков Google!

Это говорит о том, что список должен быть списком объектов (List<Object>)Так что я изменил это и несколько других вещей в адаптере!

Чтобы простить мой "List<DataForTextRow>" Я только что бросил весь список как объект!

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

Затем Android Studio не показывает ошибок, и я могу запустить приложение. Но когда я собираюсь открыть активность с упомянутыми RecyclerView это падает.

Это говорит:

DataForTextRow не может быть приведен к com.google.android.gms.ads.NativeExpressAdView

Это следующая строка onBindViewHolder

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

Поэтому для тестирования я просто прокомментировал весь случай AD_VIEW_TYPE в onBindViewHolder

Тогда я получил ошибку:

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

Это в следующей строке:

IndicatorViewHolder indicatoViewHolder = (IndicatorViewHolder)( holder);

Есть ли кто-нибудь с подобной проблемой?

Заранее спасибо!,

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 ответов

Другие вопросы по тегам