简体   繁体   中英

How to add admob using ListView and ListActivity with no layout

trying to add admob in listview using listactivity which has no layout.. need some suggestions on how to proceed..Whatever example i found is with layouts..How to do it without a layout..Here is my code..

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

Here is code to show admob in listview without adding it in layout file. Refer to this example http://googleadsdeveloper.blogspot.in/2012/03/embedding-admob-ads-within-listview-on.html for more details

  @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);
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM