0

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

King Maf
  • 42
  • 1
  • 9
  • You can use it as is, the only thing you'd require is a valid activity context, which you can pass to the Adapter via constructor. For example: `new MyAdapter adapter = MyAdapter(MainActivity.this);` – Darshan Dec 20 '21 at 07:01
  • I have found a tutorial from this video https://www.youtube.com/watch?v=Rcfpejad3PQ&list=WL. And as your Answer, I have to code valid activity context. Thanks @DarShan. – King Maf Dec 21 '21 at 00:07

1 Answers1

0

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.");
}
King Maf
  • 42
  • 1
  • 9