回收站中的Facebook原生广告视图错误

时间:2018-09-19 03:04:36

标签: android-studio android-recyclerview facebook-ads-api

我正在尝试在我的recyclerview中实现Facebook原生广告,以解析JSON中的数据。但是实施后,我在NativeAd nativeAd = (NativeAd) mExampleList.get(position);遇到了错误 在这里android studio显示警告,指出不可转换类型无法转换。 我是android的初学者,请帮助。我的示例适配器代码如下

public class ExampleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private ArrayList<ExampleItem> mExampleList;
    private onItemClickListener mListener;
    public static final int TEXT_TYPE = 0;
    public static final int IMAGE_TYPE = 1;
    public static final int NATIVE_AD = 2;
    public interface onItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(onItemClickListener listener) {
        mListener = listener;
    }

    public class ExampleViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageView;
        public TextView mTextViewCreator;
        public TextView mTextViewCat;
        public TextView mDateAll;

        public ExampleViewHolder(View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.image_view);
            mTextViewCreator = itemView.findViewById(R.id.text_view_creator);
            mTextViewCat = itemView.findViewById(R.id.text_view_cat);
            mDateAll = itemView.findViewById(R.id.date_views);
            mTextViewCreator.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            mListener.onItemClick(position);
                        }
                    }
                }
            });
        }
    }

    public class CardViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageViewz;


        public CardViewHolder(View itemView) {
            super(itemView);
            mImageViewz = itemView.findViewById(R.id.image_viewz);

        }
    }

    private static class NativeAdViewHolder extends RecyclerView.ViewHolder {
        AdIconView adIconView;
        TextView tvAdTitle;
        TextView tvAdBody;
        Button btnCTA;
        View container;
        TextView sponsorLabel;
        LinearLayout adChoicesContainer;
        MediaView mediaView;

        NativeAdViewHolder(View itemView) {
            super(itemView);
            this.container = itemView;
            adIconView = (AdIconView) itemView.findViewById(R.id.adIconView);
            tvAdTitle = (TextView) itemView.findViewById(R.id.tvAdTitle);
            tvAdBody = (TextView) itemView.findViewById(R.id.tvAdBody);
            btnCTA = (Button) itemView.findViewById(R.id.btnCTA);
            adChoicesContainer = (LinearLayout) itemView.findViewById(R.id.adChoicesContainer);
            mediaView = (MediaView) itemView.findViewById(R.id.mediaView);
            sponsorLabel = (TextView) itemView.findViewById(R.id.sponsored_label);
        }
    }

    public ExampleAdapter(Context context, ArrayList<ExampleItem> examplelist) {
        mContext = context;
        mExampleList = examplelist;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TEXT_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
                return new ExampleViewHolder(view);
            case IMAGE_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardtype, parent, false);
                return new CardViewHolder(view);
            case NATIVE_AD:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_native_ad, parent, false);
                return new CardViewHolder(view);
        }
        return null;


    }

    @Override
    public int getItemViewType(int position) {

        switch (mExampleList.get(position).getmType()) {
            case 0:
                return TEXT_TYPE;
            case 1:
                return IMAGE_TYPE;
            case 2:
                return NATIVE_AD;
            default:
                return -1;
        }


    }
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        ExampleItem object = mExampleList.get(position);
        if (object != null) {
            switch (object.getmType()) {
                case TEXT_TYPE:
                    String imageUrl = object.getmImageUrl();
                    String creatorName = object.getmCreator();
                    String cat = object.getmCat();
                    String dateall = object.getmAll();
                    ((ExampleViewHolder) holder).mTextViewCreator.setText(creatorName);
                    ((ExampleViewHolder) holder).mTextViewCat.setText(cat);
                    ((ExampleViewHolder) holder).mDateAll.setText(dateall);
                    Glide.with(mContext)
                            .load(imageUrl)
                            .into(((ExampleViewHolder) holder).mImageView);

                    break;
                case IMAGE_TYPE:
                    String imageUrlz = object.getmImageUrl();
                    Glide.with(mContext)
                            .load(imageUrlz)
                            .apply(new RequestOptions()
                                    .fitCenter())
                            .into(((CardViewHolder) holder).mImageViewz);
                    break;
                case NATIVE_AD:
                    NativeAdViewHolder nativeAdViewHolder = (NativeAdViewHolder) holder;
                    NativeAd nativeAd = (NativeAd) mExampleList.get(position);

                    AdIconView adIconView = nativeAdViewHolder.adIconView;
                    TextView tvAdTitle = nativeAdViewHolder.tvAdTitle;
                    TextView tvAdBody = nativeAdViewHolder.tvAdBody;
                    Button btnCTA = nativeAdViewHolder.btnCTA;
                    LinearLayout adChoicesContainer = nativeAdViewHolder.adChoicesContainer;
                    MediaView mediaView = nativeAdViewHolder.mediaView;
                    TextView sponsorLabel = nativeAdViewHolder.sponsorLabel;

                    tvAdTitle.setText(nativeAd.getAdvertiserName());
                    tvAdBody.setText(nativeAd.getAdBodyText());
                    btnCTA.setText(nativeAd.getAdCallToAction());
                    sponsorLabel.setText(nativeAd.getSponsoredTranslation());

                    adChoicesContainer.removeAllViews();
                    AdChoicesView adChoicesView = new AdChoicesView(mContext, nativeAd, true);


         adChoicesContainer.addView(adChoicesView);

                List<View> clickableViews = new ArrayList<>();
                clickableViews.add(btnCTA);
                clickableViews.add(mediaView);
                nativeAd.registerViewForInteraction(nativeAdViewHolder.container, mediaView, adIconView, clickableViews);
        }


    }
}

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

}

0 个答案:

没有答案