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:
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
No one has answered this question yet
Check out similar questions: