Reputation: 21
Below is my code. this code only shows interstitial ads only the first item. I want to show ads every item on my recycler view. When a user clicks on any item ads should appear.
My Adapter Code
public void onBindViewHolder(@NonNull CustomHolder holder, int position) {
final String htmlfile = htmlFile[position];
holder.textView.setText(title[position]);
MobileAds.initialize(context, "ca-app-pub-3940256099942544~3347511713");
final InterstitialAd mInterstitialAd = new InterstitialAd(context);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
Intent intent = new Intent(context,StudyActivity.class);
intent.putExtra("file",htmlfile);
context.startActivity(intent);
((CategoryActivity)context).finish();
}
});
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mInterstitialAd.isLoaded()){
mInterstitialAd.show();
}else{
Intent intent = new Intent(context,StudyActivity.class);
intent.putExtra("file",htmlfile);
context.startActivity(intent);
((CategoryActivity)context).finish();
}
}
});
}
Upvotes: 0
Views: 127
Reputation: 424
You don't have to load again in the onAdClosed method
mInterstitialAd.loadAd(new AdRequest.Builder().build());//REMOVE THIS
You don't have to initialize for every item, move the following code to your Activity
MobileAds.initialize(context, "ca-app-pub-3940256099942544~3347511713");
Upvotes: 1