How to call an Interstitial by clicking on a list item that passes through an Adapter?

Asked

Viewed 82 times

1

I need to call a Interstitial in this Activity by clicking on a list item 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();
    }

}

Man 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();
    }
}

1 answer

2


I spent days trying to implement this Interstitial (It may seem simple, but I’m a beginner in Android Development), so I decided to ask for help here, but it’s amazing, I only posted the question, minutes later I got the expected solution. I’ll leave the resolution here in case anyone ever has that doubt.

public class Adapter extends RecyclerView.Adapter {

    private ArrayList<Template> listaTemplates;
    private Context context;
    private InterstitialAd mInterstitialAd;

    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);
        }
        //holderArm.txtNome.setText("Ver Detalhes");
        final String nome = holderArm.txtNome.getText().toString();
        holderArm.txtNome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Posição na lisa e não no Banco de Dados
                Intent intent = new Intent(context, MainActivity.class);
                intent.putExtra("index", position);
                intent.putExtra("nome", nome);

                //Toast.makeText(context, "Nome printado: " + nome, Toast.LENGTH_SHORT).show();

                Bundle bundle = new Bundle();
                intent.putExtra("keyNome", nome);
                //bundle.putString("keyNome", nome);
                intent.putExtra("keyNome", nome);

                //intent.putExtra("nome", holderArm.txtNome);
                //holderArm.txtNome.toString();

                /** Código da resolução 
                    Toda vez que selecionar um item da lista que passar pelo Adapter será chamado um Interstitial
                **/
                AdRequest adRequest = new AdRequest.Builder().build();
                mInterstitialAd = new InterstitialAd(context);
                mInterstitialAd.setAdUnitId("ca-app-pub-1714562797049244/5381799713");
                mInterstitialAd.loadAd(adRequest);
                mInterstitialAd.setAdListener(new AdListener() {
                    @Override
                    public void onAdLoaded() {
                        displayInterstirial();
                    }
                });

                //Resolvendo o problema de "Calling startActivity() from outside of an Activity"
                //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

            }
        });
    }

    private void displayInterstirial(){
        if (mInterstitialAd.isLoaded()){
            mInterstitialAd.show();
        }
    }

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.