Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to set custom BaseAdapter to listview
    primarykey
    data
    text
    <p>I am trying to accomplish this: <a href="http://blog.uncommons.org/2011/05/09/embedding-admob-adverts-in-android-listviews/" rel="nofollow">http://blog.uncommons.org/2011/05/09/embedding-admob-adverts-in-android-listviews/</a>.</p> <p>I am stuck at setting the custom BaseAdapter I simply don't know what to put in the BASEADAPTER variable below.</p> <p>Here is my adapter</p> <pre><code>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&lt;String&gt; objects; public AdvertisingAdapter(Activity activity, int resource, BaseAdapter delegate, ArrayList&lt;String&gt; 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 &lt; 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 &amp;&amp; delegate.isEnabled(position - 1); } } </code></pre> <p>And in my main activity here is where I'm trying to set my custom base adapter. What should the BASEADAPTER parameter be?</p> <pre><code> mListView.setAdapter(new AdvertisingAdapter(this, android.R.layout.simple_list_item_1, BASEADAPTER, stories)); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload