0

what I am doing is when a button is pressed for the rewared ad (the button is not a part of the RecyclerView placeholder activity but is a part of the main activity that I have referenced in the view holder for my requirements), I want to replace some ImageView with another ImageView and store some shared preferences after the reward is granted. To replace the ImageView for all items in the list of the view holder, I am using a for loop which is also inside the rewarded ad method.

The code I am referring to is shown bellow. Is there a way to load the ad in the main activity instead of the view holder?

`public class MemeViewHolder extends RecyclerView.ViewHolder {
        TextView memeTextView;
        ImageView copyImageView;
        ImageView lockImageView;
        MaterialButton adBtn;
        CardView adSpace;
        YourClass yourClass = new YourClass();

        public MemeViewHolder(@NonNull View itemView) {
            super(itemView);
            memeTextView = itemView.findViewById(R.id.trollface);
            copyImageView = itemView.findViewById(R.id.copy);
            lockImageView = itemView.findViewById(R.id.lock);

            View root = binding.getWindow().getDecorView().getRootView();
            adBtn = root.findViewById(R.id.watchAdBtn);
            adSpace = root.findViewById(R.id.adSpace);
            if (mSharedPrefs.getBoolean(REWARD_GRANTED_KEY, false)) {
                adBtn.setVisibility(View.GONE);
                adSpace.setVisibility(View.VISIBLE);

            } else {
                adBtn.setVisibility(View.VISIBLE);
                adSpace.setVisibility(View.GONE);
            }
            adBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (yourClass.isNetworkConnected()) {
                        adBtn.setVisibility(View.GONE);
                        Toast.makeText(context, "Loading Ad.....", Toast.LENGTH_LONG).show();
                        // Use the test ad unit ID to load an ad.
                        AdRequest adRequest = new AdRequest.Builder().build();
                        RewardedAd.load(context, "ca-app-pub-<>",
                                adRequest, new RewardedAdLoadCallback() {
                                    @Override
                                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                                        // Handle the error.
                                        Log.d(TAG, loadAdError.toString());
                                        rewardedAd = null;
                                    }

                                    @Override
                                    public void onAdLoaded(@NonNull RewardedAd ad) {
                                        rewardedAd = ad;
                                        Log.d(TAG, "Ad was loaded.");
                                        if (rewardedAd != null) {
                                            Activity activityContext = (Activity) context;
                                            rewardedAd.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();
                                                    rewardGranted = true;
                                                    mSharedPrefs.edit().putBoolean(REWARD_GRANTED_KEY, rewardGranted).apply();
                                                    rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
                                                        @Override
                                                        public void onAdClicked() {
                                                            // Called when a click is recorded for an ad.
                                                            Log.d(TAG, "Ad was clicked.");
                                                        }

                                                        @Override
                                                        public void onAdDismissedFullScreenContent() {
                                                            // Called when ad is dismissed.
                                                            // Set the ad reference to null so you don't show the ad a second time.
                                                            Log.d(TAG, "Ad dismissed fullscreen content.");
                                                            rewardedAd = null;
                                                            adBtn.setVisibility(View.GONE);
                                                            adSpace.setVisibility(View.VISIBLE);
                                                            for (int i = 0; i < memeList.size(); i++) {
                                                                // Get the corresponding view holder for this item
                                                                RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(i);

                                                                // Update the icons for this item
                                                                if (holder instanceof MemeViewHolder) {
                                                                    MemeViewHolder itemHolder = (MemeViewHolder) holder;
                                                                    itemHolder.copyImageView.setVisibility(View.VISIBLE);
                                                                    itemHolder.lockImageView.setVisibility(View.GONE);
                                                                }
                                                            }


                                                            mSharedPrefs.edit().putBoolean(REWARD_GRANTED_KEY, rewardGranted).apply();
                                                        }

                                                        @Override
                                                        public void onAdFailedToShowFullScreenContent(AdError adError) {
                                                            // Called when ad fails to show.
                                                            Log.e(TAG, "Ad failed to show fullscreen content.");
                                                            Toast.makeText(context, "Failed to load Ad", Toast.LENGTH_SHORT).show();
                                                            rewardedAd = null;
                                                            adBtn.setVisibility(View.VISIBLE);
                                                            adSpace.setVisibility(View.GONE);

                                                        }

                                                        @Override
                                                        public void onAdImpression() {
                                                            // Called when an impression is recorded for an ad.
                                                            Log.d(TAG, "Ad recorded an impression.");
                                                        }

                                                        @Override
                                                        public void onAdShowedFullScreenContent() {
                                                            // Called when ad is shown.
                                                            Log.d(TAG, "Ad showed fullscreen content.");
                                                        }
                                                    });

                                                }
                                            });
                                        }
                                    }
                                });
                    } else {
                        Toast.makeText(context, "No network connection", Toast.LENGTH_SHORT).show();
                        adBtn.setVisibility(View.VISIBLE);
                        adSpace.setVisibility(View.GONE);
                    }
                }
            });

        }


    }`
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Mar 31 '23 at 08:47

0 Answers0