在Listview中集成Admob

时间:2018-04-03 01:45:56

标签: android listview admob

我试图在我的Android应用中实施Admob高级原生广告,无法弄清楚我做错了什么。我将广告放在Listview中。到目前为止,我成功加载了广告,但是当我将代码放在listview中时,它显示错误。这是我提出的代码:

请注意我不会把整个代码只是必要的一部分

Mainactivity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_awesome);
...
...
...
MobileAds.initialize(getApplicationContext(), getString(R.string.ad_unit_id));

private void loadNativeAd() {


  AdLoader.Builder builder = new AdLoader.Builder(this, getString(R.string.ad_unit_id));
  AdLoader adLoader = builder.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
  @Override
  public void onAppInstallAdLoaded(NativeAppInstallAd ad) {
  showshort("im ready");

        }

    }).build();

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

HomeAdapter

public class HomeAdapter extends ArrayAdapter<String>
{

Context context;

int image[];

String[] title;

String[] description;


public HomeAdapter(Context context, String[] titles, int[] imgs, String[] desc)
{

    super(context, R.layout.single_row,titles);

    this.context=context;

    this.image=imgs;

    this.title=titles;

    this.description=desc;

}

private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd,
                                      NativeAppInstallAdView adView) {


    adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline));
    adView.setBodyView(adView.findViewById(R.id.appinstall_body));
    adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action));
    adView.setIconView(adView.findViewById(R.id.appinstall_app_icon));
    adView.setPriceView(adView.findViewById(R.id.appinstall_price));
    adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars));
    adView.setStoreView(adView.findViewById(R.id.appinstall_store));

    // Some assets are guaranteed to be in every NativeAppInstallAd.
    ((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline());
    ((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody());
    ((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction());
    ((ImageView) adView.getIconView()).setImageDrawable(
            nativeAppInstallAd.getIcon().getDrawable());

    // These assets aren't guaranteed to be in every NativeAppInstallAd, so it's important to
    // check before trying to display them.
    if (nativeAppInstallAd.getPrice() == null) {
        adView.getPriceView().setVisibility(View.INVISIBLE);
    } else {
        adView.getPriceView().setVisibility(View.VISIBLE);
        ((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice());
    }

    if (nativeAppInstallAd.getStore() == null) {
        adView.getStoreView().setVisibility(View.INVISIBLE);
    } else {
        adView.getStoreView().setVisibility(View.VISIBLE);
        ((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore());
    }

    if (nativeAppInstallAd.getStarRating() == null) {
        adView.getStarRatingView().setVisibility(View.INVISIBLE);
    } else {
        ((RatingBar) adView.getStarRatingView())
                .setRating(nativeAppInstallAd.getStarRating().floatValue());
        adView.getStarRatingView().setVisibility(View.VISIBLE);
    }

    // Assign native ad object to the native view.
    adView.setNativeAd(nativeAppInstallAd);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (position == 0) {
        View row=inflater.inflate(R.layout.single_home_admob, parent, false);
        populateAppInstallAdView(nativeAppInstallAd, adView);
        return row;
    } else {

    View row=inflater.inflate(R.layout.home_fragment_single_row, parent, false);

    ImageView myimage= (ImageView) row.findViewById(R.id.icon);

    TextView mytitle= (TextView) row.findViewById(R.id.title);

    TextView mydesc= (TextView) row.findViewById(R.id.sub_title);

    myimage.setImageResource(image[position]);

    mytitle.setText(title[position]);

    mydesc.setText(description[position]);

    return row;

}
}}

single_home_admob.xml

<com.google.android.gms.ads.formats.NativeAppInstallAdView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/menu_item_card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:orientation="vertical"
    card_view:cardCornerRadius="@dimen/cardview_default_radius"
    card_view:cardElevation="@dimen/cardview_default_elevation">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FFFFFF"
        android:minHeight="50dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:textColor="#FFFFFF"
            android:textSize="12sp"
            android:width="15dp"
            android:height="15dp"
            android:background="#FFCC66"
            android:text="Ad"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="3dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/appinstall_app_icon"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:adjustViewBounds="true"
                    android:paddingBottom="5dp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/appinstall_headline"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                    <RatingBar
                        android:id="@+id/appinstall_stars"
                        style="?android:attr/ratingBarStyleSmall"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:isIndicator="true"
                        android:numStars="5"
                        android:stepSize="0.5" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/appinstall_body"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="20dp"
                    android:layout_marginRight="20dp"
                    android:textSize="12sp" />

                <com.google.android.gms.ads.formats.MediaView
                    android:id="@+id/appinstall_media"
                    android:layout_width="250dp"
                    android:layout_height="175dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="5dp" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end"
                    android:orientation="horizontal"
                    android:paddingBottom="10dp"
                    android:paddingTop="10dp">

                    <TextView
                        android:id="@+id/appinstall_price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingEnd="5dp"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingStart="5dp"
                        android:textSize="12sp" />

                    <TextView
                        android:id="@+id/appinstall_store"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingEnd="5dp"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingStart="5dp"
                        android:textSize="12sp" />

                    <Button
                        android:id="@+id/appinstall_call_to_action"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="12sp" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

我得到错误的图片

enter image description here

0 个答案:

没有答案