开发者

Adding AdView to a class that extends ListActivity

开发者 https://www.devze.com 2023-03-31 01:53 出处:网络
I\'m using AdMob for my Android apps. And I have wanted to add a AdView to an Activity that extends ListActivity.

I'm using AdMob for my Android apps. And I have wanted to add a AdView to an Activity that extends ListActivity.

I have trie开发者_JAVA技巧d using getListView().addHeaderView(my_ad_view) but that doesn't seem to work.

Any idea how to achieve this? As I'm very new to this field any help will be greatly appreciated. Thanks.

Here is my code:

This activity starts like:

public class Home extends ListActivity { /* Code */ }

The onCreate() function:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the list items. The method populateList() reads the items from the database
    // and returns a String array.
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, populateList()));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            goToTasks(String.valueOf(getListAdapter().getItem(position)));
        }
    });

    lv.setCacheColorHint(0);
    lv.setBackgroundResource(R.drawable.fmn_background_300);

    registerForContextMenu(getListView());

    refreshList();

    // Here is the AdView code
    AdView adView = new AdView(this, AdSize.BANNER, FMN.ADMOB_ID);
    LinearLayout ll = (LinearLayout)findViewById(R.id.ll_admob);
    ll.addView(adView);
    getListView().addHeaderView(ll);
    adView.loadAd(new AdRequest());
}

I have set the Permissions and have declared the activities in the Manifest as required.

In the populateList() method, I have only obtained a Cursor and then do some manipulation work and finally returned a String array.

So what am I doing wrong? The app just exits when this activity is loaded.


In case you want your add view to be always visible (so no relation to the list view) you could have a layout like the following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:id="@+id/root">

    <View  /*Your View*/ android:layout_width="fill_parent"
        android:layout_height="wrap_content"></View>
    <ListView android:id="@+id/android:list" style="@style/CustomListView" />
</LinearLayout>

In this case, you get reference to this view and you add your own view there.

In case you want to have it scrolled with your listview, you should add either as a header, or have it as a custom list item (i.e. check in your adapter if position==0 then return this AdView instead of another).

Could you also please add some more code, in order to understand what is the exact problem with your current implementation?

BR, Dimitris

0

精彩评论

暂无评论...
验证码 换一张
取 消