MyAdapter cannot be cast to com.google.android.gms.ads.NativeExpressAdView

1,928 views
Skip to first unread message

Pankaj Lilan

unread,
Mar 23, 2017, 8:42:16 AM3/23/17
to Google Mobile Ads SDK Developers

I've been working inRecyclerView and Google Volley for parsing JSON Then I added google native ads!


I following tutorial on this link


So I did change the RecyclerViewAdapter like google developers guideline!


It says that the List has to be a List of Object (List<Object>) So I changed this and a few other things in the Adapter!


Then Android Studio shows no Errors and I'm able to launch the Application. But when I'm going to open the Fragment with the mentioned RecyclerView it crashes.

It says:


java.lang.ClassCastException: com.graciaapps.nameoncards.instance.greetoptions.SubOptionOneInstance cannot be cast to com.google.android.gms.ads.NativeExpressAdView


This is the following line i onBindViewHolder


NativeExpressAdView adView = (NativeExpressAdView)greetingInstanceList.get(position);

 

Thanks in advance! 

SubOptionOneInstance.java
SubOptionOneAdapter.java
FirstFragment.java

Chris Feldman (Mobile Ads SDK Team)

unread,
Mar 23, 2017, 1:02:19 PM3/23/17
to Google Mobile Ads SDK Developers
Hi Pankaj, 

Thank you for reaching out for support. So it looks like this isn't working because you're trying to cast one of your list items that's not supposed to to be an ad to a NativeExpressAdView. You'll want to take a look at our Native Express Recycler Example to see the correct way to set up your app. Specifically, I suggest looking at our onBindViewHolder call. As well as the method we use to add the Native Express Ads to the recycler view. You need to make sure all your classes and activities are consistent with how many items there should be between each ad.

Let me know if you have any further questions.

Regards,
Chris Feldman
IMA SDK Team

Pankaj Lilan

unread,
Mar 24, 2017, 2:26:10 AM3/24/17
to Google Mobile Ads SDK Developers
Hi Chirs,

Thank you for replying, I've re-checked my code of onBindViewHolder() and addNativeExpressAds(), and also referred the example of Native Express Recycler Example. But Still, I didn't understand why the following error is occurring.

03-24 11:36:47.431 19960-19960/com.graciaapps.nameoncards E/MainActivity: The previous Native Express ad failed to load. Attempting to load the next Native Express ad in the items list.
03-24 11:36:47.432 19960-19960/com.graciaapps.nameoncards E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.graciaapps.nameoncards, PID: 19960
                                                                            java.lang.ClassCastException: Expected item at index 8 to be a Native Express ad.
                                                                                at com.graciaapps.nameoncards.fragment.greetoptions.FirstFragment.loadNativeExpressAd(FirstFragment.java:210)
                                                                                at com.graciaapps.nameoncards.fragment.greetoptions.FirstFragment.access$500(FirstFragment.java:51)
                                                                                at com.graciaapps.nameoncards.fragment.greetoptions.FirstFragment$4.onAdFailedToLoad(FirstFragment.java:232)

Chris Feldman (Mobile Ads SDK Team)

unread,
Mar 24, 2017, 1:55:45 PM3/24/17
to Google Mobile Ads SDK Developers
Hi Pankaj,

This is not actually an SDK issue. Something in your implementation is causing your RecyclerView to try and load a Native Express Ad where there isn't one. You need to make sure you're consistent with how many items are expected between each ad. If you're able to send a zip file with a build-able version of your app, I can help you to debug further. Though I'd like to reiterate that this crash is not coming from our SDK and this topic may be better suit for StackOverflow.

Regards.
Chris Feldman
Mobile Ads SDK Team

saeed shaikh

unread,
Apr 12, 2017, 5:01:20 AM4/12/17
to Google Mobile Ads SDK Developers
java.lang.ClassCastException: Expected item at index 8 to be a Native Express ad????????????????????????

Chris Feldman (Mobile Ads SDK Team)

unread,
Apr 12, 2017, 3:26:46 PM4/12/17
to Google Mobile Ads SDK Developers
Hi Saeed,

Are you seeing the same error as Pankaj? If so, my response remains the same. You need to make sure you're consistent with how many items are expected between each ad. If you're able to send a zip file with a build-able version of your app, I can help you to debug further, but this is not an SDK issue.

Regards,
Chris Feldman
Mobile Ads SDK Team

saeed shaikh

unread,
Apr 13, 2017, 5:48:05 AM4/13/17
to google-adm...@googlegroups.com
@ Chris Feldman
    Hi Chris i just sent you zip file with  build-able version of my app.
     I request you from bottom of my heart please help me.​
        

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Feldman (Mobile Ads SDK Team)

unread,
Apr 13, 2017, 2:36:06 PM4/13/17
to Google Mobile Ads SDK Developers
Hi Saeed,

Your issue stems from the fact that you're adding "posts" to your recyclerView asynchronously. Making your API calls this way is causing addNativeExpressAds(), and setUpAndLoadNativeExpressAds()to be called before getPosts()has finished. Because of this, when addNativeExpressAds() is called, mRecyclerViewItems.size() is still zero and your for loop doesn't add any NativeExpressAdViews to your recyclerView. Then setUpAndLoadNativeExpressAds() tries to load ads in an adView that doesn't exist and you're hit with the ClassCastException.

Let me know if you have any further questions. We also saw that you posted again on this thread. For any follow-ups, we ask that you respond only here.

Regards,
Chris Feldman
Mobile Ads SDK Team

Jitander Verma

unread,
May 18, 2017, 9:57:20 AM5/18/17
to Google Mobile Ads SDK Developers
Hi Chris,
I am having the same issue as i am also using Google Volley and Recyclerview for asynchronous loading of items. Can You Help me what should I do Now?. 
Thanks In Advance
To post to this group, send email to google-adm...@googlegroups.com.

Deepika Uragayala- MobileAds SDK team

unread,
May 18, 2017, 3:11:38 PM5/18/17
to Google Mobile Ads SDK Developers
Hi Jitander,

This error simply means that the Adapter is expecting a View that is not yet loaded/ available. Purely it is an implementation issue and as my colleague Chris in the previous responses here has suggested, make sure you are calling the ad requests on the proper index of the array. If you are changing the items in the list dynamically, then an ad received might make it to the wrong index, which might not contain the original ad view that the request was made for.

Also, I could see that you have posted this question in several threads. I would like you to continue the discussion in this thread from now on if you still continue to have issues.

Regards,
Deepika Uragayala
Mobile Ads SDK Team

Jitander Verma

unread,
May 23, 2017, 10:44:05 AM5/23/17
to google-adm...@googlegroups.com
Hello Deepika,
First of all, Thank You allot for quick help. :). Actually I knew, Purely it is a implementation issue. All I was is looking for some help regarding this. Because after lot of effort (Almost 15 days), I am not getting anywhere.
Thank You Again.

To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.

Deepika Uragayala- MobileAds SDK team

unread,
May 23, 2017, 2:57:13 PM5/23/17
to Google Mobile Ads SDK Developers
Hi Jitander,

We would be happy to assist you with this and to be able to do that, can you share your sample app with us first?

You can send us privately as well if you want to.

Regards,
Deepika Uragayala
Mobile Ads SDK Team

On Tuesday, May 23, 2017 at 10:44:05 AM UTC-4, Jitander Verma wrote:
Hello Deepika,
First of all, Thank You allot for quick help. :). Actually I knew, Purely it is a implementation issue. All I was is looking for some help regarding this. Because after lot of effort (Almost 15 days), I am not getting anywhere.
Thank You Again.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jitander Verma

unread,
May 23, 2017, 3:31:16 PM5/23/17
to google-adm...@googlegroups.com

Hi Deepika, 
Thank You alott.  Sure, How can i send you my app code personally? Can you share email mam?
Thank you again 😊


To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Deepika Uragayala- MobileAds SDK team

unread,
May 24, 2017, 3:37:45 PM5/24/17
to Google Mobile Ads SDK Developers
Hi Jitander,

Thanks for sharing the project. After looking into your project I could see that you are adding NativeAdView(s) without waiting for your JSON response and the mRecyclerViewItems array is messed up. You are adding the AdView for every 4th iteration, but the array is never ready to handle those. I would suggest that you wait for the JSON to be added into your array, then iterate over every ITEMS_PER_AD, add your AdView, set the adapter and then make the AdRequest. Let me know if that helps.

Regards,
Deepika Uragayala
Mobile Ads SDK Team

On Tuesday, May 23, 2017 at 3:31:16 PM UTC-4, Jitander Verma wrote:

Hi Deepika, 
Thank You alott.  Sure, How can i send you my app code personally? Can you share email mam?
Thank you again 😊

To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jitander Verma

unread,
May 26, 2017, 12:36:29 AM5/26/17
to google-adm...@googlegroups.com
Hii Deepika,
Thank You for support. Ohhkkkk, Now I understand where is the problem. Let me make changes into my project. I will surely let you know about it mam. 
Thank You again.
Jitander Verma
Android Developer and founder
Sctar Labs.

To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jitander Verma

unread,
Jun 3, 2017, 6:59:15 AM6/3/17
to google-adm...@googlegroups.com
Hi Deepika,
Good Evening,
Can You please help me with code. How can i make wait for json to be added to array. I follow stack overflow whole week also do lot of code. but still not getting anywhere.
Thanks in Advance.
Jitander Verma.

On Fri, May 26, 2017 at 10:06 AM, Jitander Verma <jitande...@gmail.com> wrote:
Hii Deepika,
Thank You for support. Ohhkkkk, Now I understand where is the problem. Let me make changes into my project. I will surely let you know about it mam. 
Thank You again.
Jitander Verma
Android Developer and founder
Sctar Labs.

Jitander Verma

unread,
Jun 4, 2017, 4:46:29 AM6/4/17
to google-adm...@googlegroups.com
Hi Deepika,
Its Working Now. All is do is call methods inside onResponse() method which were loading ads in NativeExpressAdviews.. 
Thank You Alot for support.

Pankaj Lagad

unread,
Jun 27, 2017, 2:56:42 PM6/27/17
to Google Mobile Ads SDK Developers

Hello Guys, 

I am also facing save error, I just want to implement 3 view types in my activity. 1. MENU_ITEM, 2. NATIVE_EXPRESS_ADS every 8th position & 3. LOAD_MORE_VIEW for every 10th position.


But its giving me following error: 


java.lang.ClassCastException: com.ex.lp.rv.root.SecondPage.Data.TVShow cannot be cast to com.google.android.gms.ads.NativeExpressAdView
                                                                                                   at com.ex.lp.rv.root.SecondPage.MyAdapterSecondPage.onBindViewHolder(MyAdapterSecondPage.java:113)


Please take look of my code:

// MyAdapterSecondPage.java

public class MyAdapterSecondPage extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

Context c;
Vibrator vib;
String root = Environment.getExternalStorageDirectory().toString();
public static int myClickedImageId;
ConnectivityManager cm;
NetworkInfo activeNetwork;
boolean isConnected;
String packageName = "";
public String urlLink;
public int imageId;
private final ArrayList<Object> mRecyclerViewItems;
private static final int MENU_ITEM_VIEW_TYPE = 0;
private static final int NATIVE_EXPRESS_AD_VIEW_TYPE = 1;
private final int VIEW_TYPE_LOADING = 2;
private boolean isLoading;
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private static MainActivity ma = new MainActivity();
private OnLoadMoreListener onLoadMoreListener;

public MyAdapterSecondPage(Context c, ArrayList<Object> recyclerViewItems) {
this.c = c;
this.mRecyclerViewItems = recyclerViewItems;

final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) ma.rv.getLayoutManager();
ma.rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
isLoading = true;
}
}
});

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == MENU_ITEM_VIEW_TYPE) {
Log.d("onCreateViewHolder", "" + "MENU_ITEM_VIEW_TYPE");
View menuItemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.quote_grid_model_default, parent, false);
return new MyHolder(menuItemLayoutView);
} else if (viewType == NATIVE_EXPRESS_AD_VIEW_TYPE) {
Log.d("onCreateViewHolder", "" + "NATIVE_EXPRESS_AD_VIEW_TYPE");
View nativeExpressLayoutView = LayoutInflater.from(
parent.getContext()).inflate(R.layout.native_express_ad_container,
parent, false);
return new NativeExpressAdViewHolder(nativeExpressLayoutView);
} else {
Log.d("onCreateViewHolder", "" + "VIEW_TYPE_LOADING");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_loading, parent, false);
return new LoadingViewHolder(view);
}

}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
if (viewType == MENU_ITEM_VIEW_TYPE) {
Log.d("onBindViewHolder", "" + "MENU_ITEM_VIEW_TYPE");
MyHolder menuItemHolder = (MyHolder) holder;
PicassoClient.downloadImage(c, ((TVShow) mRecyclerViewItems.get(position)).getUrl().toString().trim(), menuItemHolder.img);
menuItemHolder.nameTxt.setText(((TVShow) mRecyclerViewItems.get(position)).getName().toString().trim());
} else if (viewType == NATIVE_EXPRESS_AD_VIEW_TYPE) {
Log.d("onBindViewHolder", "" + "NATIVE_EXPRESS_AD_VIEW_TYPE");
NativeExpressAdViewHolder nativeExpressHolder =
(NativeExpressAdViewHolder) holder;
NativeExpressAdView adView =
(NativeExpressAdView) (mRecyclerViewItems.get(position));
ViewGroup adCardView = (ViewGroup) nativeExpressHolder.itemView;
if (adCardView.getChildCount() > 0) {
adCardView.removeAllViews();
}
if (adView.getParent() != null) {
((ViewGroup) adView.getParent()).removeView(adView);
}
adCardView.addView(adView);
} else if (viewType == VIEW_TYPE_LOADING) {
Log.d("onBindViewHolder", "" + "VIEW_TYPE_LOADING");
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setIndeterminate(true);
}
}


public class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView img;
TextView nameTxt;
public MyAdapterFirstPage myAdapterFirstPage = new MyAdapterFirstPage(null, null);

public MyHolder(View itemView) {
super(itemView);
nameTxt = (TextView) itemView.findViewById(R.id.nameTxt);
img = (ImageView) itemView.findViewById(R.id.movieImage);

urlLink = myAdapterFirstPage.setUrlLink;
imageId = 1;
packageName = "/FacebookImageSharingDir/ActressPhotos";

File FacebookImageSharingDirectoryObject = new File(root + packageName);
if (!FacebookImageSharingDirectoryObject.exists()) {
FacebookImageSharingDirectoryObject.mkdirs();
}

img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isConnected = isConnected();
Log.d("myClickedImageId", "" + myClickedImageId);

if (isConnected) {
myClickedImageId = getAdapterPosition() + imageId;
// Intent i = new Intent(c, MainActivityImageView.class);
// c.startActivity(i);
} else {
Toast.makeText(getApplicationContext(), "" + "Sorry! Not Connected To Internet.", Toast.LENGTH_LONG).show();
}
}
});
}

@Override
public void onClick(View view) {
MyAdapterFirstPage myAdapterFirstPage = new MyAdapterFirstPage(null, null);
try {
vib = (Vibrator) c.getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(13);
} catch (Exception e) {
e.printStackTrace();
}

urlLink = myAdapterFirstPage.setUrlLink;
imageId = 1;

}
}

public class NativeExpressAdViewHolder extends RecyclerView.ViewHolder {

NativeExpressAdViewHolder(View view) {
super(view);
}
}

@Override
public int getItemCount() {
if (mRecyclerViewItems == null) {
return 0;
} else {
return mRecyclerViewItems.size();
}
}


@Override
public int getItemViewType(int position) {
if (mRecyclerViewItems.get(position) == null) {
return VIEW_TYPE_LOADING;
} else {
if (position % MainActivity.ITEMS_PER_AD != 0) {
return MENU_ITEM_VIEW_TYPE;
} else {
return NATIVE_EXPRESS_AD_VIEW_TYPE;
}
}
}


public boolean isConnected() {
cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
}


private class LoadingViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;

public LoadingViewHolder(View view) {
super(view);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);
}
}


public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
this.onLoadMoreListener = mOnLoadMoreListener;
}

public void setLoaded() {
isLoading = false;
}


}

//  MainActivity.java

public class MainActivity extends AppCompatActivity {
public static RecyclerView rv;
MyAdapterSecondPage adapter;
private static final int REQUEST_READ_WRITE_PERMISSION = 786;
public static ShareDialog shareDialog;
public CallbackManager callbackManager;
public LoginManager manager;
public List<String> permissionNeeds;

public static final int ITEMS_PER_AD = 8;
private static final int NATIVE_EXPRESS_AD_HEIGHT = 150;
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/1072772517";
private ArrayList<Object> mRecyclerViewItems = new ArrayList<>();
MyAdapterFirstPage myAdapterFirstPage = new MyAdapterFirstPage(null, null) ;
TVShow tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quote_grid_activity_main);
requestPermission();

rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));

if (getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
rv.setLayoutManager(new GridLayoutManager(getApplicationContext(), 1));
} else {
rv.setLayoutManager(new GridLayoutManager(getApplicationContext(), 1));
}

addImages();
addNativeExpressAds();
setUpAndLoadNativeExpressAds();


adapter = new MyAdapterSecondPage(getApplicationContext(), mRecyclerViewItems);
rv.setAdapter(adapter);


//set load more listener for the RecyclerView adapter
adapter.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
if (mRecyclerViewItems.size() <= 20) {
Log.d("Iaminset", ""+"I am in setOnLoadMoreListener "+ mRecyclerViewItems.size());
mRecyclerViewItems.add(null);
adapter.notifyItemInserted(mRecyclerViewItems.size() - 1);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mRecyclerViewItems.remove(mRecyclerViewItems.size() - 1);
adapter.notifyItemRemoved(mRecyclerViewItems.size());

//Generating more data
int index = mRecyclerViewItems.size();
int end = index + 10;
for (int i = index; i < end; i++) {
tv = new TVShow();
tv.setUrl(myAdapterFirstPage.setUrlLink + "(" + i + ").jpg");
tv.setName("" + i);

mRecyclerViewItems.add(tv);
}
adapter.notifyDataSetChanged();
adapter.setLoaded();

}
}, 5000);
} else {
Toast.makeText(MainActivity.this, "Loading data completed", Toast.LENGTH_SHORT).show();
}
}
});



}

private void addImages() {
MyAdapterFirstPage myAdapterFirstPage = new MyAdapterFirstPage(null, null) ;
TVShow tv;

for (int i = 1; i <= 10; ++i) {
tv = new TVShow();
tv.setUrl(myAdapterFirstPage.setUrlLink + "(" + i + ").jpg");
tv.setName("" + i);

mRecyclerViewItems.add(tv);
}

}

private void addNativeExpressAds() {
for (int i = 0; i <= mRecyclerViewItems.size(); i += ITEMS_PER_AD) {
final NativeExpressAdView adView = new NativeExpressAdView(MainActivity.this);
mRecyclerViewItems.add(i, adView);
}
}


private void setUpAndLoadNativeExpressAds() {
rv.post(new Runnable() {
@Override
public void run() {
final float scale = MainActivity.this.getResources().getDisplayMetrics().density;
// Set the ad size and ad unit ID for each Native Express ad in the items list.
Log.d("MRVSIZE", ""+mRecyclerViewItems.size());
for (int i = 0; i <= mRecyclerViewItems.size(); i += ITEMS_PER_AD) {
final NativeExpressAdView adView =
(NativeExpressAdView) mRecyclerViewItems.get(i);
final CardView cardView = (CardView) findViewById(R.id.ad_card_view);
final int adWidth = cardView.getWidth() - cardView.getPaddingLeft()
- cardView.getPaddingRight();
AdSize adSize = new AdSize((int) (adWidth / scale), NATIVE_EXPRESS_AD_HEIGHT);
adView.setAdSize(adSize);
adView.setAdUnitId(AD_UNIT_ID);
}

// Load the first Native Express ad in the items list.
loadNativeExpressAd(0);
}
});
}

private void loadNativeExpressAd(final int index) {

if (index >= mRecyclerViewItems.size()) {
return;
}

Object item = mRecyclerViewItems.get(index);
if (!(item instanceof NativeExpressAdView)) {
throw new ClassCastException("Expected item at index " + index + " to be a Native"
+ " Express ad.");
}

final NativeExpressAdView adView = (NativeExpressAdView) item;
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
loadNativeExpressAd(index + ITEMS_PER_AD);
}

@Override
public void onAdFailedToLoad(int errorCode) {
Log.e("MainActivity", "The previous Native Express ad failed to load. Attempting to"
+ " load the next Native Express ad in the items list.");
loadNativeExpressAd(index + ITEMS_PER_AD);
}
});

// Load the Native Express ad.
adView.loadAd(new AdRequest.Builder().build());
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
Log.e("mydata", data.toString());
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_READ_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Toast.makeText(MainActivity.this, "Welcome!!!", Toast.LENGTH_SHORT).show();
}
}

private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_WRITE_PERMISSION);

} else {

}
}

}

// TVShow.java

public class TVShow  {

String name;
String url;


public TVShow() {

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

}





On Sunday, June 4, 2017 at 1:46:29 AM UTC-7, Jitander Verma wrote:
Hi Deepika,
Its Working Now. All is do is call methods inside onResponse() method which were loading ads in NativeExpressAdviews.. 
Thank You Alot for support.

On Sat, Jun 3, 2017 at 4:28 PM, Jitander Verma <jitande...@gmail.com> wrote:
Hi Deepika,
Good Evening,
Can You please help me with code. How can i make wait for json to be added to array. I follow stack overflow whole week also do lot of code. but still not getting anywhere.
Thanks in Advance.
Jitander Verma.
On Fri, May 26, 2017 at 10:06 AM, Jitander Verma <jitande...@gmail.com> wrote:
Hii Deepika,
Thank You for support. Ohhkkkk, Now I understand where is the problem. Let me make changes into my project. I will surely let you know about it mam. 
Thank You again.
Jitander Verma
Android Developer and founder
Sctar Labs.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.

Jitander Verma

unread,
Jun 27, 2017, 4:40:12 PM6/27/17
to google-adm...@googlegroups.com
Hii,
Make sure to load complete recyclerview items before loading any Native ads.


To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsub...@googlegroups.com.
To post to this group, send email to google-admob-ads-sdk@googlegroups.com.

Deepika Uragayala- MobileAds SDK team

unread,
Jun 27, 2017, 6:14:40 PM6/27/17
to Google Mobile Ads SDK Developers
Hi Jitander,

Thanks for chiming in and that was a good suggestion.

Hey Pankaj,

This is an issue with how you're injecting views into the RecyclerView. It is not an SDK issue. Given the complexity of your implementation, I would suggest posting this on StackOverflow. Your app is crashing because you're trying to load an ad in your 'com.ex.lp.rv.root.SecondPage.Data.TVShow' view. Make sure that you try to implement that in the adview. 

If you have any other issues with our SDK, do let us know and we will be happy to assist you.

Regards,
Deepika Uragayala
Mobile Ads SDK Team


Jitander Verma

unread,
Jun 27, 2017, 6:20:23 PM6/27/17
to google-adm...@googlegroups.com
Hi Deepika,
Your Welcome mam :)


To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-adm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to a topic in the Google Groups "Google Mobile Ads SDK Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-admob-ads-sdk/DTMvw_aPkPE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-admob-ads-sdk+unsubscrib...@googlegroups.com.

To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pankaj Lagad

unread,
Jun 29, 2017, 10:09:39 AM6/29/17
to google-adm...@googlegroups.com
Hello Sir,

Please provide some explanation I am new in Android programming.

Thank you.

To post to this group, send email to google-admob-ads-sdk@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mayank Kutmutia

unread,
Sep 21, 2017, 8:41:08 AM9/21/17
to Google Mobile Ads SDK Developers
Hi Deepika,

I have been going through the discussion and I followed the code as it is here is my implementation, could you please help me out

package com.noticeboard.domain.noticeboard;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.android.gms.ads.NativeExpressAdView;

import java.util.List;

/**
* Created by MayankKutmutia on 8/9/2017.
*/

public class ActivitesTwoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Object> mRecyclerViewItems;
private static final int AD_VIEW_TYPE=1;
private static final int MENU_ITEM_VIEW_TYPE=0;

private Context context;
private int lastPosition = -1;

public ActivitesTwoAdapter(Context context,List<Object> aca) {
this.mRecyclerViewItems = aca;
this.context = context;
}


public static class MenuViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView mTextView,mTextView1,mTextView2,mTextView3,mTextView4;
public CardView cardView;
public ImageView imageView;




public MenuViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.info_text);
mTextView1 = (TextView) v.findViewById(R.id.info_text1);
mTextView2 = (TextView) v.findViewById(R.id.info_text2);
mTextView3 = (TextView) v.findViewById(R.id.info_text3);
mTextView4 = (TextView) v.findViewById(R.id.info_text4);
imageView = (ImageView) v.findViewById(R.id.info_text5);
cardView = (CardView) v.findViewById(R.id.card_view);

}
}

public class NativeExpressAds extends RecyclerView.ViewHolder{
public NativeExpressAds(View view){

super(view);
}
}
@Override
public int getItemCount() {
        return mRecyclerViewItems.size();

}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
        switch (viewType) {


case MENU_ITEM_VIEW_TYPE:
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.act_two_item, parent, false);
// set the view's size, margins, paddings and layout parameters

return new MenuViewHolder(v);
case AD_VIEW_TYPE:
default:
View nativeexpress = LayoutInflater.from(parent.getContext()).inflate(R.layout.act_two_native,parent,false);

return new NativeExpressAds(nativeexpress);

}

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {
int viewType = getItemViewType(position);
switch (viewType){
case MENU_ITEM_VIEW_TYPE:

MenuViewHolder menuViewHolder = (MenuViewHolder) holder;
final ActTwoTiles actTwoTiles = (ActTwoTiles) mRecyclerViewItems.get(position);
menuViewHolder.mTextView.setText(actTwoTiles.getTitle());
menuViewHolder.mTextView1.setText(actTwoTiles.getCompany());
menuViewHolder.mTextView2.setText(actTwoTiles.getDate());
menuViewHolder.mTextView3.setText(actTwoTiles.getAmount());
menuViewHolder.mTextView4.setText(actTwoTiles.getShortDescription());
Glide.with(context).load(actTwoTiles.getSourceLogo()).into(menuViewHolder.imageView);
menuViewHolder.cardView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
                Uri uri = Uri.parse(actTwoTiles.getLink());
Intent websiteIntent = new Intent(Intent.ACTION_VIEW, uri);
v.getContext().startActivity(websiteIntent);

}
});
break;

/* Animation animation = AnimationUtils.loadAnimation(context,
(position > lastPosition) ? R.anim.up_from_bottom_act
: R.anim.down_from_top_act);
holder.itemView.startAnimation(animation);
lastPosition = position;*/
case AD_VIEW_TYPE:

default:
NativeExpressAds nativeExpressAds = (NativeExpressAds) holder;
NativeExpressAdView nativeExpressAdView = (NativeExpressAdView) mRecyclerViewItems.get(position);
ViewGroup adCardView = (ViewGroup) nativeExpressAds.itemView;
if (adCardView.getChildCount() > 0) {
adCardView.removeAllViews();
}
if (nativeExpressAdView.getParent() != null) {
((ViewGroup) nativeExpressAdView.getParent()).removeView(nativeExpressAdView);
}

// Add the Native Express ad to the native express ad view.
adCardView.addView(nativeExpressAdView);


}

}







@Override
public int getItemViewType(int position) {
        return (position%6 == 0)? AD_VIEW_TYPE:MENU_ITEM_VIEW_TYPE;
}
}

Hope you help me out soon.

Regards.

Deepika Uragayala- MobileAds SDK team

unread,
Sep 21, 2017, 10:47:42 AM9/21/17
to Google Mobile Ads SDK Developers
Hi Mayank, 

In your onBindViewHolder() function, you have a SWITCH control flow, where you have case MENU_ITEM_VIEW_TYPE, AD_VIEW_TYPE and then default. The AdView has been bind to the AD_VIEW_TYPE case but you are inflating the View and making an AdRequest via the default case. This is not the correct way. Move the holder inflation code into the AD_VIEW_TYPE case in the control flow and it will work.

Let us know if you need anything else.


Regards,
Deepika Uragayala
Mobile Ads SDK Team
Reply all
Reply to author
Forward
0 new messages