gpt4 book ai didi

java - 尝试将自定义 BaseAdapter 设置为 listview

转载 作者:太空宇宙 更新时间:2023-11-04 15:21:33 26 4
gpt4 key购买 nike

我正在努力实现这一目标: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);
}
}

在我的主要 Activity 中,我尝试设置我的自定义基本适配器。 BASEADAPTER 参数应该是什么?

 mListView.setAdapter(new AdvertisingAdapter(this,
android.R.layout.simple_list_item_1, BASEADAPTER, stories));

最佳答案

这取决于您尝试在 ListView 中显示或显示的内容,通常应用程序上下文和所需数据将传递给BaseAdapter构造函数。有关详细信息,请阅读 BaseAdapter甚至检查 Android ListView详细教程。

关于java - 尝试将自定义 BaseAdapter 设置为 listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20343242/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com