如何使用没有布局的ListView和ListActivity添加admob

时间:2015-01-11 06:01:10

标签: android listview layout admob listactivity

尝试使用没有布局的listactivity在listview中添加admob ..需要一些关于如何继续的建议..我发现的任何一个例子都是布局..怎么做没有布局..这是我的代码..

      import android.app.ListActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.ArrayAdapter;
      import android.widget.ListView;
      import android.widget.Toast;

      public class MyListActivity extends ListActivity {
      public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
      "Linux", "OS/2" };
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, values);
      setListAdapter(adapter);
      }

     @Override
     protected void onListItemClick(ListView l, View v, int position, long id) {
     String item = (String) getListAdapter().getItem(position);
     Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
     }
    } 

1 个答案:

答案 0 :(得分:0)

这是在listview中显示admob而不在布局文件中添加它的代码。有关详细信息,请参阅此示例http://googleadsdeveloper.blogspot.in/2012/03/embedding-admob-ads-within-listview-on.html

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if ((position % k) == 0) {
      if (convertView instanceof AdView) {
        // Don’t instantiate new AdView, reuse old one
        return convertView;
      } else {
        // Create a new AdView
        AdView adView = new AdView(activity, AdSize.BANNER,
                                   INSERT_ADMOB_ID_HERE);

        // Convert the default layout parameters so that they play nice with
        // ListView.

        float density = activity.getResources().getDisplayMetrics().density;
        int height = Math.round(AdSize.BANNER.getHeight() * density);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
            AbsListView.LayoutParams.FILL_PARENT,
            height);
        adView.setLayoutParams(params);

        adView.loadAd(new AdRequest());
        return adView;
      }
    } else {
      // Offload displaying other items to the delegate
      return delegate.getView(position - (int) Math.ceil(position / k) - 1,
          convertView, parent);
    }
  }