Search code examples
androidandroid-arrayadaptercustom-adapter

Add custom view (ad space) programmatically in ArrayAdapter


I'm trying to add a custom view programmatically into a listview of images in the custom adapter. Currently I'm trying to add it as the third "image". Not that steady on my customer adapter, so not sure how to get it to work. Populating all the images works perfect, but cant get that ad space in between there. Any suggestions?

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.image_row, null);
        ViewHolder viewHolder = new ViewHolder();

        if(position==3) {
            adSpace = new AdSpace(context, "c026dd7e-60a5-46c9-948f-38eb18a", true);
            adSpace.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));

            Log.i("AdSpace","Adding adspace");
        } else {
            viewHolder.image = (ImageView) rowView.findViewById(R.id.offer_row);
        }
        rowView.setTag(viewHolder);
    }



    ViewHolder holder = (ViewHolder) rowView.getTag();
    imageLoader.displayImage(imageURLArray.get(position), holder.image, options);

    return rowView;

}
static class ViewHolder {
    public ImageView image;
    public AdSpace adspace;
}

list.xml:

<?xml version="1.0" encoding="utf-8"?>
<"packagename".ZoomView xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
    <ListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/offer_list" />    

image_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/"packagename""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">  
<ImageView
    android:id="@+id/offer_row"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:contentDescription="Image" />  


Solution

  • You are creating adSpace but never using it.

    if(position==3) {
            adSpace = new AdSpace(context, "c026dd7e-60a5-46c9-948f-38eb18a", true);
            adSpace.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    
            Log.i("AdSpace","Adding adspace");
            return adSpace;
        } 
    

    You can remove adSpace from the viewholder.