如何在列表视图中添加原生广告? [英] How to add native ads in a listview?

查看:207
本文介绍了如何在列表视图中添加原生广告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的活动 我想将原生广告插入列表视图. 我正在尝试遵循本指南 https://github.com/StartApp -SDK/Documentation/wiki/android-advanced-usage 但我觉得很难理解. 您能帮我一下吗,也许可以编写代码示例?谢谢

this is my activity i want to insert a native ads into the list view. I'm trying to follow this guide https://github.com/StartApp-SDK/Documentation/wiki/android-advanced-usage But I find it hard to understand. can you give me a hand, maybe making examples of code? thank you

活动

public class EpisodiActivity extends Activity {

private StartAppAd startAppAd = new StartAppAd(this);

public class ViewModel {
    private String url;
    private String name;

    public ViewModel(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public String getUrl() {
        return this.url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return this.name;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // creazione fullscreen activity
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.episodi_activity);

    String[] episodi = getIntent().getStringArrayExtra("Product");
    String[] urls = getIntent().getStringArrayExtra("urls");

    ListView mylist = (ListView) findViewById(R.id.listView1);

    // And in this loop we create the ViewModel instances from
    // the name and url and add them all to a List
    List<ViewModel> models = new ArrayList<ViewModel>();
    for (int i = 0; i < episodi.length; i++) {
        String name = episodi[i];
        String url = "No value";
        if (i < urls.length) {
            url = urls[i];
        }
        ViewModel model = new ViewModel(url, name);
        models.add(model);
    }

    // Here we create the ArrayAdapter and assign it to the ListView
    // We pass the List of ViewModel instances into the ArrayAdapter
    final ArrayAdapter<ViewModel> adapter = new ArrayAdapter<ViewModel>(
            this, android.R.layout.simple_list_item_1, models);

    mylist.setAdapter(adapter);

    mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View v, int position,
                long id) {

            // Here we get the ViewModel at the given position
            ViewModel model = (ViewModel) arg0.getItemAtPosition(position);

            // And the url from the ViewModel
            String url = model.getUrl();

            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    });
}

@Override
public void onResume() {
    super.onResume();
    startAppAd.onResume();
    startAppAd.showAd();
}

@Override
public void onPause() {
    super.onPause();
    startAppAd.onPause();
}

}

推荐答案

listView BaseAdapter首先调用方法getCount()检索要显示的行数,然后针对每行调用getView(int position, View convertView, ViewGroup parent)方法以创建并返回给定位置的View对象.

A listView BaseAdapter first calls the method getCount() to retrieve the number of rows to show and then for each row it calls the getView(int position, View convertView, ViewGroup parent) method to create and return a View object for the given position.

方法是扩展BaseAdapter并创建自己的自定义适配器,而不是使用默认的ArrayAdapter.然后,当您要展示广告时,您需要返回一个广告"视图,而不是通常的普通视图.这意味着您需要覆盖getCount()以返回更多行(例如,如果您有10行,则意味着您需要返回11 = 10个实际内容+ 1个广告)

The approach is to extend BaseAdapter and create your own custom Adapter, instead of using the default ArrayAdapter. Then you need to return an "Ad" View instead of your usual normal View when you want to display an ad. This means that you need to override getCount() to return more rows (for example if you have 10 rows, that means you need to return 11 = 10 actual content + 1 ad)

然后您需要确定创建此视图的位置,我认为您可以通过简单地检查position变量来做到这一点:

Then you need to decide in which position to create this View, I think you can do it by simply checking the position variable:

if (position == VALUE) {
   // Create and return Ad View 
} else {
   // Create and return a normal View 
}

无论如何,这整个过程确实很棘手,因为事情很容易失控(位置与视图不匹配等). StartApp应该能够控制您的listView适配器为您完成所有这些操作.我的猜测是您的适配器无法与StartApp正确通信(也许您未正确初始化?).

Anyway, this whole thing is really tricky as things can go easily out of hand (positions mismatching with Views etc). StartApp should be able to control your listView adapter to do all this for you. My guess is that your adapter is not communicating properly with StartApp (maybe you are not initialising correctly?).

尝试深入阅读文档或从中查找示例.如果您不知道,可以使用其他替代方法,例如Avocarrot,Namomedia,Inmobi等

Try to dig into the documentation or find an example by them. If you can’t figure it out, there are other alternatives you can use such as Avocarrot, Namomedia, Inmobi, etc

我使用了 Avocarrot ,该代码在github中有一个开放源代码示例,可用于插入listViews中的广告. 您可以运行它并在适合时使用它: https://github.com/Avocarrot/android -demo-app

I have used Avocarrot which has an open source example in github for inserting ads in listViews. You can run it and use it if it fits you: https://github.com/Avocarrot/android-demo-app

这篇关于如何在列表视图中添加原生广告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆