0
votes

I have created twoe viewHolders in my RecyclerView class, one for the contents and the other for facebook native ads, My app clash immediately after running it. How do I overcome this?

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return new ViewHolderBoxOffice(layoutInflater.from(parent.getContext()).inflate(R.layout.custom_movie_box_office, parent, false));
            case 2:

                return new NativeAdViewHolder(layoutInflater.from(parent.getContext()).inflate(R.layout.custom_ads_facebook, parent, false));

        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case 0:
                ViewHolderBoxOffice v_holder = (ViewHolderBoxOffice) holder;
                Movie currentMovie = mlistMovies.get(position);
                v_holder.movieTitle.setText(currentMovie.getTitle());
                Date movieReleaseDate = currentMovie.getReleaseDateTheater();
                if (movieReleaseDate != null) {
                    String formmattedDate = dateFormat.format(movieReleaseDate);
                    v_holder.movieReleaseDate.setText(formmattedDate);
                } else {
                    v_holder.movieReleaseDate.setText(Constants.NA);

                }
                int audienceScore = currentMovie.getAudienceScore();
                if (audienceScore == -1) {
                    v_holder.movieAudienceScore.setRating(0.0F);
                    v_holder.movieAudienceScore.setAlpha(0.5F);
                } else {
                    v_holder.movieAudienceScore.setRating(currentMovie.getAudienceScore() / 20.0F);
                    v_holder.movieAudienceScore.setAlpha(1.0F);
                }
                String urlThumbnail = currentMovie.getUrlThumbnail();
                loadImages(urlThumbnail, v_holder);
            case 2:
                NativeAdViewHolder mHolder=(NativeAdViewHolder)holder;
                // Setting the Text.

                nativeAd.unregisterView();
                mHolder.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
                mHolder.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
                mHolder.nativeAdTitle.setText(nativeAd.getAdTitle());
                mHolder.nativeAdBody.setText(nativeAd.getAdBody());

                // Downloading and setting the ad icon.
                NativeAd.Image adIcon = nativeAd.getAdIcon();
                NativeAd.downloadAndDisplayImage(adIcon, mHolder.nativeAdIcon);

                // Download and setting the cover image.
                NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
                mHolder.nativeAdMedia.setNativeAd(nativeAd);


                // Add adChoices icon

               // if (adChoicesView == null)

                if(! (mHolder.adview.getChildAt(0) instanceof AdChoicesView)){
                   AdChoicesView adChoicesView = new AdChoicesView(layoutInflater.getContext(), nativeAd, true);

                    mHolder.adview.addView(adChoicesView, 0);
                }

                nativeAd.registerViewForInteraction( mHolder.adview);

                default:
                    break;
        }
        if (position > previousPosition) {
            comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);

        } else {
            comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);

        }
        previousPosition = position;

    }



    private void loadImages(String urlThumbnail, final ViewHolderBoxOffice holder) {
        if (urlThumbnail != null) {
            imageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
                @Override
                public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                    holder.movieThumbnail.setImageBitmap(response.getBitmap());
                }

                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
        }
    }


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

    @Override
    public void onError(Ad ad, AdError adError) {

    }

    @Override
    public void onAdLoaded(Ad ad) {


    }

    @Override
    public void onAdClicked(Ad ad) {
        L.m("Ads is loaded, Jeez man that is good");
        System.out.println("Loaded in fragment");
        nativeAd = manager.nextNativeAd();

        nativeAd.setAdListener(this);



    }
    @Override
    public int getItemViewType(int position) {
        int viewType = 0;
        if (position % 5 == 1)
            viewType = 2;
        return viewType;
    }

    @Override
    public void onAdsLoaded() {
        L.m("Ads is loaded, Jeez man that is good");
        System.out.println("Loaded in fragment");
        nativeAd = manager.nextNativeAd();

        nativeAd.setAdListener(this);


    }

    @Override
    public void onAdError(AdError adError) {

    }

    class NativeAdViewHolder extends RecyclerView.ViewHolder {
        private ImageView nativeAdIcon;
        private TextView nativeAdTitle;
        private TextView nativeAdBody;
        private MediaView nativeAdMedia;
        private TextView nativeAdSocialContext;
        private Button nativeAdCallToAction;
        private LinearLayout adview;


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

// Create native UI using the ad metadata.
            nativeAdIcon = (ImageView) itemView.findViewById(R.id.native_ad_icon);
            nativeAdTitle = (TextView) itemView.findViewById(R.id.native_ad_title);
            nativeAdBody = (TextView) itemView.findViewById(R.id.native_ad_body);
            nativeAdMedia = (MediaView) itemView.findViewById(R.id.native_ad_media);
            nativeAdSocialContext = (TextView) itemView.findViewById(R.id.native_ad_social_context);
            nativeAdCallToAction = (Button) itemView.findViewById(R.id.native_ad_call_to_action);
            adview=(LinearLayout) itemView.findViewById(R.id.ads_unit);


        }
    }


    class ViewHolderBoxOffice extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView movieThumbnail;
        private TextView movieTitle;
        private TextView movieReleaseDate;
        private RatingBar movieAudienceScore;

        public ViewHolderBoxOffice(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            movieThumbnail = (ImageView) itemView.findViewById(R.id.movieThumbnail);
            movieTitle = (TextView) itemView.findViewById(R.id.movieTitle);
            movieReleaseDate = (TextView) itemView.findViewById(R.id.movieReleaseDate);
            movieAudienceScore = (RatingBar) itemView.findViewById(R.id.movieAudienceScore);


        }

Here is the error.

01-31 16:30:48.944 9429-9429/comeagain.materialdesign E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: comeagain.materialdesign, PID: 9429
                                                                        java.lang.ClassCastException: comeagain.materialdesign.adapters.AdapterBoxOffice$ViewHolderBoxOffice cannot be cast to comeagain.materialdesign.adapters.AdapterBoxOffice$NativeAdViewHolder
                                                                            at comeagain.materialdesign.adapters.AdapterBoxOffice.onBindViewHolder(AdapterBoxOffice.java:148)
                                                                            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)

Error is at line

 NativeAdViewHolder mHolder=(NativeAdViewHolder)holder;

I unable to determine what is the problem and how to solve this issue. Kindly help.

1

1 Answers

2
votes

Terminate your Switch case with a break.

 @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case 0:
            ViewHolderBoxOffice v_holder = (ViewHolderBoxOffice) holder;
            Movie currentMovie = mlistMovies.get(position);
            v_holder.movieTitle.setText(currentMovie.getTitle());
            Date movieReleaseDate = currentMovie.getReleaseDateTheater();
            if (movieReleaseDate != null) {
                String formmattedDate = dateFormat.format(movieReleaseDate);
                v_holder.movieReleaseDate.setText(formmattedDate);
            } else {
                v_holder.movieReleaseDate.setText(Constants.NA);

            }
            int audienceScore = currentMovie.getAudienceScore();
            if (audienceScore == -1) {
                v_holder.movieAudienceScore.setRating(0.0F);
                v_holder.movieAudienceScore.setAlpha(0.5F);
            } else {
                v_holder.movieAudienceScore.setRating(currentMovie.getAudienceScore() / 20.0F);
                v_holder.movieAudienceScore.setAlpha(1.0F);
            }
            String urlThumbnail = currentMovie.getUrlThumbnail();
            loadImages(urlThumbnail, v_holder);
            break;
        case 2:
            NativeAdViewHolder mHolder=(NativeAdViewHolder)holder;
            // Setting the Text.

            nativeAd.unregisterView();
            mHolder.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
            mHolder.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
            mHolder.nativeAdTitle.setText(nativeAd.getAdTitle());
            mHolder.nativeAdBody.setText(nativeAd.getAdBody());

            // Downloading and setting the ad icon.
            NativeAd.Image adIcon = nativeAd.getAdIcon();
            NativeAd.downloadAndDisplayImage(adIcon, mHolder.nativeAdIcon);

            // Download and setting the cover image.
            NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
            mHolder.nativeAdMedia.setNativeAd(nativeAd);


            // Add adChoices icon

           // if (adChoicesView == null)

            if(! (mHolder.adview.getChildAt(0) instanceof AdChoicesView)){
               AdChoicesView adChoicesView = new AdChoicesView(layoutInflater.getContext(), nativeAd, true);

                mHolder.adview.addView(adChoicesView, 0);
            }

            nativeAd.registerViewForInteraction( mHolder.adview);
            break;
          default:
                break;
    }
    if (position > previousPosition) {
        comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);

    } else {
        comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);

    }
    previousPosition = position;

}

And next time use a debbuger to find stuff like this.