0
votes

We are trying to display admob ads in listview. We may show ads in every 10 objects so we are trying to preload 3 ads at the time of onCreate(). Following is the code snippet for loading ads.

AdRequest request = new AdRequest.Builder()
                .addTestDevice("C6736622852CDF40B5FC25A9DF38DAAB")
                .build();
final  NativeExpressAdView adView = (NativeExpressAdView)View.inflate(context, R.layout.admob_ad_view, null);
adView.loadAd(request);
adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                listOfAds.add(adView);
                }
}

With above code, I am able to create 3 adviews and store in a listview, while user scrolls down the listview, we are loading ads from this list. We create a empty linear layout for recyclerview element and in onBind(), we are adding this adView object to that linear layout.

linearlayout.addView(adView);

ISSUE: It works fine for 75% of the time. Other times, it shows an empty cell. When user tries to scroll or tap on the empty cell, adview gets refreshed and displays the ad perfectly.

<com.google.android.gms.ads.NativeExpressAdView
    android:id="@+id/adView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adUnitId="@string/SHORT_ADMOB_AD_UNIT_ID"
    ads:adSize="FULL_WIDTHx80">
</com.google.android.gms.ads.NativeExpressAdView>

compile('com.google.firebase:firebase-ads:9.2.0') { force = true; }

What would be the issue?

1

1 Answers

0
votes

Found the problem with my code. In mode based on the logic, it was refreshing the row so frequently by calling method notifyDataSetChanged(). Even when one row was changing, code was refreshing all rows. This caused the problem with admob ad view. Fixed that logic to selectively refresh rows. (Excluding ad row)