I'm implementing Facebook Native banner ads in my ListView
. The ads are working fine but the empty layout for the ads is also being shown above the ads.
Below is my ListView
's adapter code:
public CouponsViewAdapter(Context context, List<DataSetForCoupons>
couponsdata, String amount_value, String id, String value,
List<DataSetForAddCoupons> addcouponsdata, boolean saving) {
this.context = context;
this.DataList = couponsdata;
this.DataListAdd = addcouponsdata;
this.amount = amount_value;
this.id = id;
this.value = value;
this.saving = saving;
for (int i = 0; i < DataList.size(); i++) {
if (i % 2 == 0 && i != 0) {
itemType.add(i, 1);
DataList.add(i, null);
} else
itemType.add(i, 0);
}
}
@Override
public int getCount() {
return DataList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
if (itemType.get(position) == 1)
return SECOND_ITEM;
else
return FIRST_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
if (convertView == null) {
final LayoutInflater Inflater = (LayoutInflater) context.getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
switch (type) {
case SECOND_ITEM:
nativeBannerAd = new NativeBannerAd(context, "IMG_16_9_APP_INSTALL#635165165156968704093");
convertView = Inflater.inflate(R.layout.banner_ad, null, false);
nativeBannerAdContainer = convertView.findViewById(R.id.banner_ad_layout);
nativeBannerAd.setAdListener(new NativeAdListener() {
@Override
public void onAdLoaded(Ad ad) {
if (nativeBannerAd == null || nativeBannerAd != ad) {
return;
}
nativeBannerAd.unregisterView();
adView = (LinearLayout) Inflater.inflate(R.layout.banner_ad,
nativeBannerAdContainer, false);
nativeBannerAdContainer.addView(adView);
RelativeLayout adChoicesContainer = adView.findViewById(R.id.ad_choices_container);
AdChoicesView adChoicesView = new AdChoicesView(context, nativeBannerAd, true);
adChoicesContainer.addView(adChoicesView, 0);
AdIconView nativeAdIconView = adView.findViewById(R.id.native_icon_view);
Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);
List<View> clickableViews = new ArrayList<>();
clickableViews.add(nativeAdTitle);
clickableViews.add(nativeAdCallToAction);
nativeBannerAd.registerViewForInteraction(adView, nativeAdIconView, clickableViews);
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onLoggingImpression(Ad ad) {
}
});
nativeBannerAd.loadAd();
break;
case FIRST_ITEM:
//Implementing the ListView here
break;
return convertView;
}
I tried to use convertView
instead of adView
, but it produces errors on runtime.
Here is the screenshot
Before adding new views You need to remove previously added views from the existing view neither it may cause duplicate view nativeBannerAdContainer.removeAllViews(); // Add this line of code before adding new views nativeBannerAdContainer.addView(adView); //
nativeBannerAd.setAdListener(new NativeAdListener() {
@Override
public void onAdLoaded(Ad ad) {
if (nativeBannerAd == null || nativeBannerAd != ad) {
return;
}
nativeBannerAd.unregisterView();
adView = (LinearLayout) Inflater.inflate(R.layout.banner_ad,
nativeBannerAdContainer, false);
nativeBannerAdContainer.removeAllViews();
nativeBannerAdContainer.addView(adView);
RelativeLayout adChoicesContainer = adView.findViewById(R.id.ad_choices_container);
AdChoicesView adChoicesView = new AdChoicesView(context, nativeBannerAd, true);
adChoicesContainer.addView(adChoicesView, 0);
AdIconView nativeAdIconView = adView.findViewById(R.id.native_icon_view);
Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);
List<View> clickableViews = new ArrayList<>();
clickableViews.add(nativeAdTitle);
clickableViews.add(nativeAdCallToAction);
nativeBannerAd.registerViewForInteraction(adView, nativeAdIconView, clickableViews);
}
@Override
public void onAdClicked(Ad ad) {
}
@Override
public void onLoggingImpression(Ad ad) {
}
});