简体   繁体   中英

Rewarded Video Ads version 20.0.0 above in Adapter

I'm new in Java and I want to implement Reward Video Ads version 20.0.0 or above in Adapter.class. Everything runs well as I read in this link https://developers.google.com/admob/android/rewarded , till I found the codes to show reward ads. The codes look like this if in MainActivity.class

if (mRewardedAd != null) {
  Activity activityContext = MainActivity.this;
  mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
    @Override
    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
      // Handle the reward.
      Log.d(TAG, "The user earned the reward.");
      int rewardAmount = rewardItem.getAmount();
      String rewardType = rewardItem.getType();
    }
  });
} else {
  Log.d(TAG, "The rewarded ad wasn't ready yet.");
}

My question is, how to implement the codes above in Adapter.class?

Thanks for your answers. It will help me so much.

Edit: To make the question clearer, I want to implement Rewarded Video Ad in every list of RecyclerView

All you have to do is send your Activity to the constructor method.

I have found a complete tutorial from this link . As DarShan's answer, I have to adjust the codes for valid activity context. And All runs well. Here are my codes from the tutorial. I change MainActivity.this to valid activity

if (mRewardedAd != null) {
  Activity activityContext = activity;
  mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
    @Override
    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
      // Handle the reward.
      Log.d(TAG, "The user earned the reward.");
      int rewardAmount = rewardItem.getAmount();
      String rewardType = rewardItem.getType();
    }
  });
} else {
  Log.d(TAG, "The rewarded ad wasn't ready yet.");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM