I need to call an Interstitial in this Activity by clicking on an item in the list that comes from my Adapter :
public class ListOfTemplates extends AppCompatActivity {
private Template template;
private ViewHolder view;
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_templates);
final RecyclerView rclTemplates = findViewById(R.id.rclTemplates);
final TextView txtNome = findViewById(R.id.txtNome);
final Button btn = findViewById(R.id.btnAdmod);
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
//Toast.makeText(getApplicationContext(), "Desliguei", Toast.LENGTH_LONG).show();
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
LoadInterstitial();
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
LoadInterstitial();
}
});
rclTemplates.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
//mInterstitialAd.show();
}
});
if (ListaTemplates.getTamanhoListaTemplates() > 0){
ListaTemplates.getListaTemplates().clear();
}
TemplateDAO dao = new TemplateDAO(getApplicationContext());
dao.selectTemplates();
ListaTemplates.carregarTemplates();
Adapter tempAdapter = new Adapter(ListaTemplates.getListaTemplates(), getApplicationContext());
rclTemplates.setAdapter(tempAdapter);
RecyclerView.LayoutManager meuLayout = new LinearLayoutManager(
getApplicationContext(),
LinearLayoutManager.VERTICAL,
false
);
rclTemplates.setLayoutManager(meuLayout);
}
private void LoadInterstitial(){
//mInterstitialAd.loadAd(new AdRequest.Builder().build());
AdRequest interAdRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(interAdRequest);
Toast.makeText(getApplicationContext(), "Estou aqui", Toast.LENGTH_LONG).show();
}
}
My Adapter :
public class Adapter extends RecyclerView.Adapter {
private ArrayList<Template> listaTemplates;
private Context context;
public Adapter(ArrayList<Template> listaTemplates, Context context) {
this.listaTemplates = listaTemplates;
this.context = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.minha_celula, parent, false);
//
RecyclerView.ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
final ViewHolder holderArm = (ViewHolder)holder;
holderArm.txtNome.setText(listaTemplates.get(position).getNome());
if (ListaTemplates.getTemplate(position).getPhoto().equals("classic")){
holderArm.imgCelula.setImageResource(R.drawable.on);
}else if (ListaTemplates.getTemplate(position).getPhoto().equals("spark"){
holderArm.imgCelula.setImageResource(R.drawable.power_on2);
}
final String nome = holderArm.txtNome.getText().toString();
holderArm.txtNome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("index", position);
intent.putExtra("nome", nome);
Bundle bundle = new Bundle();
intent.putExtra("keyNome", nome);
intent.putExtra("keyNome", nome);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return listaTemplates.size();
}
}