如何在列表视图中添加原生广告?

joh*_*ohn 4 android listview ads

这是我的活动,我想在列表视图中插入原生广告.我正在尝试按照本指南https://github.com/StartApp-SDK/Documentation/wiki/android-advanced-usage但我觉得很难理解.你可以帮我一把,也许可以举出代码示例吗?谢谢

活动

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();
}
Run Code Online (Sandbox Code Playgroud)

}

cod*_*y89 5

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

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

然后你需要决定创建这个View的位置,我想你可以通过简单地检查position变量来做到这一点:

if (position == VALUE) {
   // Create and return Ad View 
} else {
   // Create and return a normal View 
}
Run Code Online (Sandbox Code Playgroud)

无论如何,整个事情真的很棘手,因为事情很容易失控(与视图等不匹配的位置).StartApp应该能够控制你的listView适配器为你做这一切.我的猜测是你的适配器与StartApp无法正常通信(也许你没有正确初始化?).

尝试深入了解文档或找到他们的示例.如果您无法弄明白,还可以使用其他替代品,如Avocarrot,Namomedia,Inmobi等

我使用了Avocarrot,它在github中有一个开源示例,用于在listViews中插入广告.你可以运行它并使用它,如果它适合你:https://github.com/Avocarrot/android-demo-app