Admob Native ads with RecyclerView crashed when item deleted in RecyclerView

299 views Asked by At

**I am displaying native ads in RecyclerView ** There is 2 types of items in RecyclerView, one is NativeExpressAdView and 2nd item is Some Object.I have added Ads item in list after every 3 objects.It is working fine until i delete the item from the List and call notifyDataSetChanged() in RecyclerView, after calling notifyDataSetChanged() the position of of every object changed and code getting messed up please take a look at the code and tell me what i can do to resolve the issue.

// A menu item view type.
private static final int MENU_ITEM_VIEW_TYPE = 0;

// The Native Express ad view type.
private static final int NATIVE_EXPRESS_AD_VIEW_TYPE = 1;

public DailyAlarmAdapter(Context context, List<Object> givenPostAndAds, int day) {
    mContext = context;
    alarmList = new ArrayList<>(givenPostAndAds);
    db = new DatabaseHandler(mContext);
    this.day = day;
}

/**
 * Determines the view type for the given position.
 */
@Override
public int getItemViewType(int position) {
    // Show list of post
    return (position % NavMainActivity.ITEMS_PER_AD == 0) ? NATIVE_EXPRESS_AD_VIEW_TYPE
            : MENU_ITEM_VIEW_TYPE;

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    // Post layout
    switch (viewType) {
        case MENU_ITEM_VIEW_TYPE:
            View menuItemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
                    R.layout.class_design, viewGroup, false);
            //menuItemLayoutView.setOnClickListener(this);
            ListingHolder holder = new ListingHolder(menuItemLayoutView);
            menuItemLayoutView.setTag(holder);
            return holder;
        case NATIVE_EXPRESS_AD_VIEW_TYPE:
            // fall through
        default:
            View nativeExpressLayoutView = LayoutInflater.from(
                    viewGroup.getContext()).inflate(R.layout.native_express_ad_container,
                    viewGroup, false);
            return new NativeExpressAdViewHolder(nativeExpressLayoutView);
    }

}

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


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    int viewType = getItemViewType(position);
    switch (viewType) {
        case MENU_ITEM_VIEW_TYPE:
            **//Here Item type is Menu Item but now at this position there is NativeExpressAdView**
            if (alarmList.get(position) instanceof NativeExpressAdView)
                return;
            ListingHolder listingHolder = (ListingHolder) holder;
            configurePostView(listingHolder, position);
            break;
        case NATIVE_EXPRESS_AD_VIEW_TYPE:
        default:
            **//Here Item type is AD_VIEW_TYPE but now at this position there is Alarm Object**
            if (alarmList.get(position) instanceof Alarm)
                return;
            NativeExpressAdViewHolder nativeExpressHolder =
                    (NativeExpressAdViewHolder) holder;
            NativeExpressAdView adView =
                    (NativeExpressAdView) alarmList.get(position);
            ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView;
            // The NativeExpressAdViewHolder recycled by the RecyclerView may be a different
            // instance than the one used previously for this position. Clear the
            // NativeExpressAdViewHolder of any subviews in case it has a different
            // AdView associated with it, and make sure the AdView for this position doesn't
            // already have a parent of a different recycled NativeExpressAdViewHolder.
            if (adCardView.getChildCount() > 0) {
                adCardView.removeAllViews();
            }
            if (adView.getParent() != null) {
                ((ViewGroup) adView.getParent()).removeView(adView);
            }

            // Add the Native Express ad to the native express ad view.
            adCardView.addView(adView);
    }

}
0

There are 0 answers