How to use native AdMod ads with Recycler view?

I want to display ads using Google Admob in my application. Main activity. I have put all the code to show ads, getting help from google developers site, but when I run my app no โ€‹โ€‹ads are displayed. I do not know what's the problem:

enter image description here

MainActivity:

package com.ali.nokt_nokt.mdhk.glopal;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;

import com.ali.nokt_nokt.mdhk.R;
import com.ali.nokt_nokt.mdhk.adaptr.Post;
import com.ali.nokt_nokt.mdhk.adaptr.PostsAdapter;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;

public MainActivity extends AppCompatActivity
{
    RecyclerView recyclerview;
    PostsAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.face);
        //Set back button on action bar
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);




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

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.tagarb.esy.es/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        retrofit.create(API.class).getbast_B().enqueue(new Callback<List<Post>>() {
            @Override
            public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                final List<Post> postList = response.body();
                adapter = new PostsAdapter(postList, Bast_2.this);
                recyclerview.setAdapter(adapter);

                final Post post = new Post();
                post.setTitle("ู„ุญู† ุงู„ุญูŠุงู‡");
                post.setBody("ูˆู‚ู€ุงู„ุช ู„ู€ูŠ ุงู„ุฃุฑุถู - ู„ู…ู€ุง ุณู€ุฃู„ุช:\tุฃูŠู€ู€ุง ุฃู…ูู‘ ู‡ู€ู€ู„ ุชูƒู€ู€ุฑู‡ูŠู† ุงู„ุจุดู€ู€ุฑู’?\n" +
                        "ุฃูุจู€ู€ุงุฑูƒ ูู€ูŠ ุงู„ู†ู€ุงุณ ุฃู‡ู€ู„ูŽ ุงู„ุทู…ู€ูˆุญ\tูˆู…ู€ู€ู† ูŠุณู€ู€ุชู„ุฐูู‘ ุฑูƒู€ู€ูˆุจูŽ ุงู„ุฎู€ู€ุทุฑู’\n" +
                        "ูˆุฃู„ู’ุนู€ู€ู†ู ู…ู€ู€ู† ู„ุง ูŠู…ุงุดู€ู€ูŠ ุงู„ุฒู…ู€ุงู†ูŽ\tูˆูŠู‚ู†ู€ู€ู€ุน ุจู€ู€ุงู„ุนูŠู’ุดู ุนูŠุดู ุงู„ุญุฌูŽู€ู€ุฑู’\n" +
                        "ู‡ู€ู€ูˆ ุงู„ูƒู€ู€ูˆู†ู ุญู€ูŠูŒู‘, ูŠุญู€ุจูู‘ ุงู„ุญูŠู€ุงุฉ\tูˆูŠุญู€ู€ุชู‚ุฑ ุงู„ู…ูŽูŠู’ู€ู€ุชูŽ, ู…ู‡ู…ู€ู€ุง ูƒู€ู€ุจูุฑู’\n" +
                        "ูู€ู„ุง ุงู„ุฃูู’ู€ู‚ ูŠุญู€ุถู† ู…ูŠู’ู€ุชูŽ ุงู„ุทูŠู€ูˆุฑู\tูˆู„ุง ุงู„ู†ุญู€ู€ู„ู ูŠู„ุซู€ู€ู… ู…ูŠู’ู€ู€ุชูŽ ุงู„ุฒู‡ู€ุฑู’\n" +
                        "ูˆู„ู€ู€ูˆู„ุง ุฃู…ููˆู…ู€ู€ุฉู ู‚ู„ุจูู€ู€ูŠ ุงู„ู€ู€ุฑู‘ุคูˆู…\tู„ูŽู…ูŽู€ู€ุง ุถู…ู‘ู€ู€ุชู ุงู„ู…ูŠู’ู€ุชูŽ ุชู„ู€ูƒ ุงู„ุญูู€ููŽุฑู’\n" +
                        "ูู€ู€ูˆูŠู„ูŒ ู„ู…ู€ู€ู† ู„ู€ู€ู… ุชุดูู€ู€ู‚ู‡ ุงู„ุญูŠู€ุง\tุฉ, ู…ูู€ู€ู† ู„ุนู†ู€ู€ุฉ ุงู„ุนู€ู€ุฏู… ุงู„ู…ู†ุชุตูู€ุฑู’!");

                new CountDownTimer(4000, 1000) {

                    @Override
                    public void onTick(long millisUntilFinished) {

                    }

                    @Override
                    public void onFinish() {
                        postList.set(0, post);
                        adapter.notifyItemChanged(0);
                    }
                }.start();
            }

            @Override
            public void onFailure(Call<List<Post>> call, Throwable t) {

            }
        });
    }


    public interface API {
        @GET("bast_B.php")
        Call<List<Post>> getbast_B();
    }



    //Options Menu Create
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    //Options Menu on Item Click
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

//Take back to Home Screen on pressing back button
        if (id == android.R.id.home) {

//Simply finish this activity
            Bast_2.this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);

    }
}

      

And this is MyAdapter.java:

package com.ali.nokt_nokt.mdhk.adaptr;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ali.nokt_nokt.mdhk.R;



import java.util.List;

/**
 * Created by developerhendy on 9/8/16.
 * ยฉ 2016.
 */
public class PostsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {



    private List<Post> posts;
    private Context context;

    public PostsAdapter(List<Post> posts, Context ctx) {
        this.posts = posts;
        this.context = ctx;
    }




    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 456) {
            return new AdsHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.ads_row, parent, false));

        } else {
            return new PostHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.post_row, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if (viewType == 456) {
            AdsHolder adsHolder = (AdsHolder) holder;



            // if we wanna do some thing with ads row for example change img we do it here



        } else {
            PostHolder postHolder = (PostHolder) holder;

            if (viewType == 123) {
                postHolder.postLayout.setBackgroundColor(Color.parseColor("#23c486"));
                postHolder.postTitle.setTextColor(Color.parseColor("#ffffff"));
                postHolder.postDesc.setTextColor(Color.parseColor("#ffffff"));
            }

            final Post post = posts.get(position);
            postHolder.postTitle.setText(post.getTitle());
            postHolder.postDesc.setText(post.getBody());



        }

    }





    @Override
    public int getItemCount() {
        return posts.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 2) {
            return 123;
        } else if (position == 3) {
            return 456;
        } else {
            return super.getItemViewType(position);
        }
    }



    public class AdsHolder extends RecyclerView.ViewHolder {

        public AdsHolder(View itemView) {
            super(itemView);
        }
    }

    public class PostHolder extends RecyclerView.ViewHolder {
        private TextView postTitle;
        private TextView postDesc;
        private LinearLayout postLayout;


        public PostHolder(final View itemView) {
            super(itemView);
            postTitle = (TextView) itemView.findViewById(R.id.post_title);
            postDesc = (TextView) itemView.findViewById(R.id.post_desc);
            postLayout = (LinearLayout) itemView.findViewById(R.id.row_container);


            postTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Post post = posts.get(getAdapterPosition());
                    Toast.makeText(context, "You clicked on " + post.getTitle(), Toast.LENGTH_SHORT).show();
                }
            });

            postLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Post post = posts.get(getAdapterPosition());

                    Intent i = new Intent(context, Pag.class);
                    i.putExtra("Titler", post.getTitle());
                    i.putExtra("Desc", post.getBody());
                    context.startActivity(i);
                }
            });



        }
    }
    }

      

And this activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ececec"
    tools:context="com.ali.nokt_nokt.mdhk.glopal.Bast_2">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

      

and this admob.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"

    android:orientation="vertical" android:layout_width="360dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="8dp"
    android:layout_height="132dp">


    <com.google.android.gms.ads.AdView

        android:id="@+id/adView"
        android:layout_width="360dp"
        android:layout_height="132dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>


</android.support.v7.widget.CardView>

      

+3


source to share





All Articles