Re: Google AdMob ads within ListView Android

1,349 views
Skip to first unread message

William Ferguson

unread,
May 8, 2013, 9:27:56 AM5/8/13
to google-adm...@googlegroups.com
This has got nothing to do with Admob. It is a ListView question.

You need to look at line#44 of WeatherAdapter.java

William


On Tuesday, May 7, 2013 5:46:53 PM UTC+10, 3ncrypt0 wrote:
Hi,


I don`t understand "else {
      // Offload displaying other items to the delegate
      return delegate.getView(position - (int) Math.ceil(position / k) - 1,
          convertView, parent); 
}"

So, i have created 2 CustomBaseAdapters. The first adapter is the adapter with the normal list_items. The second adapter is the adAdapter. But, if i run my application, it crashed.

05-07 09:36:39.766: E/AndroidRuntime(23157): FATAL EXCEPTION: main
05-07 09:36:39.766: E/AndroidRuntime(23157): java.lang.NullPointerException
05-07 09:36:39.766: E/AndroidRuntime(23157): at com.example.androidwithinlistview.WeatherAdapter.getView(WeatherAdapter.java:44)
05-07 09:36:39.766: E/AndroidRuntime(23157): at com.example.androidwithinlistview.ListViewExampleListAdapter.getView(ListViewExampleListAdapter.java:53)
05-07 09:36:39.766: E/AndroidRuntime(23157): at android.widget.AbsListView.obtainView(AbsListView.java:2143)
05-07 09:36:39.766: E/AndroidRuntime(23157): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)


And i have no idea why...

Here is my first adapter:

"public class WeatherAdapter extends BaseAdapter{

    Context context; 
    int layoutResourceId;    
    Weather data[] = null;
    
    public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;
        
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            
            holder = new WeatherHolder();
            holder.txtTitle = (TextView)row.findViewById(R.id.textView1);
            
            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }
        
        Weather weather = data[position];
        holder.txtTitle.setText(weather.title);
        
        return row;
    }
    
    static class WeatherHolder
    {
        TextView txtTitle;
    }

@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}"


this is my second adapter (where i add the ads):

"public class ListViewExampleListAdapter extends BaseAdapter implements AdListener {
private int k = 3;
private Activity activity;
private static final String ADMOB_ID = "1234567890";
private WeatherAdapter delegate;
public ListViewExampleListAdapter(Activity activity, WeatherAdapter delegate) {
this.activity = activity;
this.delegate = delegate;
}
@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, ADMOB_ID);

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

@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
}

@Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}

@Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}

@Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}

@Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
}"

and my MainActivity:


public class MainActivity extends Activity {
private ListView listView1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Weather weather_data[] = new Weather[]
       {
           new Weather("Cloudy"),
           new Weather("Showers"),
           new Weather("Snow"),
           new Weather("Storm"),
           new Weather("Sunny")
       };
WeatherAdapter adapter = new WeatherAdapter(this, 
                R.layout.list_item, weather_data);
ListViewExampleListAdapter adAdapter = new ListViewExampleListAdapter(this, adapter);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adAdapter);
}
}


Can anybody help me?

Thanks

Reply all
Reply to author
Forward
0 new messages