1

I have implemented a rewarded ad feature in my app, where clicking on a lock icon shows an ad and on successful completion of the ad, I want to replace all the lock icons with copy icons in the entire list. However, the current code I have for replacing the icons requires the use of a RecyclerView variable, and it needs to be instantiated in the adapter method, causing issues in the MainActivity since I'm only using the list and context there. I apologize for any grammar errors, I've done my best to explain the situation.

Here is the code

private final RecyclerView recyclerView;


    public MemeAdapter(List<String> memeList, Context mContext, RecyclerView recyclerView) {
        this.memeList = memeList;
        this.context = mContext;
        this.rewardGranted = recyclerView;

        mSharedPrefs = context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
        rewardGranted = mSharedPrefs.getBoolean(REWARD_GRANTED_KEY, false);
    }

inside the bindviewholder

 @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.");
                                    mRewardedVideoAd = null;
                                    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 MemeAdapter.MemeViewHolder) {
                                            MemeAdapter.MemeViewHolder itemHolder = (MemeAdapter.MemeViewHolder) holder;
                                            itemHolder.copyImageView.setVisibility(View.VISIBLE);
                                            itemHolder.lockImageView.setVisibility(View.GONE);
                                        }
                                    }

//                                    holder.copyImageView.setVisibility(View.VISIBLE);
//                                    holder.lockImageView.setVisibility(View.GONE);
                                    mSharedPrefs.edit().putBoolean(REWARD_GRANTED_KEY, rewardGranted).apply();
                                }

Her is the error im getting

error: constructor MemeAdapter in class MemeAdapter cannot be applied to given types;
        MemeAdapter adapter = new MemeAdapter(asciiArts, this);
                              ^
  required: List<String>,Context,RecyclerView
  found: List<String>,MainActivity
  reason: actual and formal argument lists differ in length
Vasant Raval
  • 257
  • 1
  • 12
  • 31

1 Answers1

1

All the updates of recyclerview should be caused by the adapter - that's the "single source of truth" for the recycler. The basic idea is that when you call notifyXxxChanged you tell the adapter that some data changed and you want to update the display. It will call onBindViewHolder for all the changed data allowing you to choose a new way to display it.

As a starting point you can do this:

Store data as List<Pair<String, Boolean>> memeList. The first element of the pair is the string you previously stored. The second one is a boolean flag indicating which image to show.

Update stored data inside onAdDismissedFullScreenContent:

@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.");
    mRewardedVideoAd = null;
    for (int i = 0; i < memeList.size(); i++) {
        memeList[i].second = true;
    }
    notifyDataRangeChanged(0, memeList.size()); // tell adapter to update all data. More efficient than notifyDatasetChanged
    mSharedPrefs.edit().putBoolean(REWARD_GRANTED_KEY, rewardGranted).apply();
}

Inside onBindViewHolder display correct image:

@Override
public void onBindViewHolder(...) {
    ...
    if (memeList[position].second) {
        itemHolder.copyImageView.setVisibility(View.VISIBLE);                              
        itemHolder.lockImageView.setVisibility(View.GONE);
    } else {
        itemHolder.copyImageView.setVisibility(View.GONE);                              
        itemHolder.lockImageView.setVisibility(View.VISIBLE);
    }
}
Sergei Kozelko
  • 691
  • 4
  • 17