我正在努力实现这一目标:http://blog.uncommons.org/2011/05/09/embedding-admob-adverts-in-android-listviews/。
我在设置自定义BaseAdapter时遇到困难我根本不知道在下面的BASEADAPTER变量中放入什么。
这是我的适配器
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
/**
* List adapter decorator that inserts adverts into the list.
* @author Daniel Dyer
*/
public class AdvertisingAdapter extends BaseAdapter
{
private static final String ADMOB_PUBLISHER_ID = "---------------";
private final Activity activity;
private final BaseAdapter delegate;
private int resource;
private ArrayList<String> objects;
public AdvertisingAdapter(Activity activity, int resource, BaseAdapter delegate,
ArrayList<String> stories) {
// TODO Auto-generated constructor stub
this.resource = resource;
this.activity = activity;
this.delegate = delegate;
this.objects = stories;
delegate.registerDataSetObserver(new DataSetObserver()
{
@Override
public void onChanged()
{
notifyDataSetChanged();
}
@Override
public void onInvalidated()
{
notifyDataSetInvalidated();
}
});
}
public int getCount()
{
return delegate.getCount() + 1;
}
public Object getItem(int i)
{
return delegate.getItem(i - 1);
}
public long getItemId(int i)
{
return delegate.getItemId(i - 1);
}
public View getView(int position, View convertView, ViewGroup parent)
{
if ((position % 10) == 0)
{
if (convertView instanceof AdView)
{
return convertView;
}
else
{
AdView adView = new AdView(activity, AdSize.BANNER, ADMOB_PUBLISHER_ID);
// Disable focus for sub-views of the AdView to avoid problems with
// trackpad navigation of the list.
for (int i = 0; i < adView.getChildCount(); i++)
{
adView.getChildAt(i).setFocusable(false);
}
adView.setFocusable(false);
// Default layout params have to be converted to ListView compatible
// params otherwise there will be a ClassCastException.
float density = activity.getResources().getDisplayMetrics().density;
int height = Math.round(AdSize.BANNER.getHeight() * density);
AbsListView.LayoutParams params
= new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
height);
adView.setLayoutParams(params);
adView.loadAd(new AdRequest());
return adView;
}
}
else
{
return delegate.getView(position - 1, convertView, parent);
}
}
@Override
public int getViewTypeCount()
{
return delegate.getViewTypeCount() + 1;
}
@Override
public int getItemViewType(int position)
{
return position == 0 ? delegate.getViewTypeCount()
: delegate.getItemViewType(position - 1);
}
@Override
public boolean areAllItemsEnabled()
{
return false;
}
@Override
public boolean isEnabled(int position)
{
return position != 0 && delegate.isEnabled(position - 1);
}
}
在我的主要活动中,我正在尝试设置自定义基本适配器。 BASEADAPTER参数应该是什么?
mListView.setAdapter(new AdvertisingAdapter(this,
android.R.layout.simple_list_item_1, BASEADAPTER, stories));
答案 0 :(得分:0)
它依赖于您尝试在列表视图中显示或显示的内容,通常应用程序上下文和期望数据将传递给BaseAdapter constructor
。有关详细信息,请阅读BaseAdapter,甚至查看Android ListView详细教程。