views:

144

answers:

1

Folks -

I'm trying to implement a Gallery widget that displays an ArrayList of images, and I have started with the Hello, Gallery example on the dev site. This part is all working great.

I need to have the gallery display an empty view (a special view when the ArrayList has no contents), but I cannot seem to get the Gallery to do this. I have done this with ListView and other AdapterViews in the past, but I cannot get it to work with Gallery. What do I need to override/implement in the Adapter, Gallery, or both to get an empty view displayed? This is my adapter code:

public class ImageAdapter extends BaseAdapter {

     int mGalleryItemBackground;
     private Context mContext;
     private ArrayList<Drawable> images;

     public ImageAdapter(Context c) {
      mContext = c;
      TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
      mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
      a.recycle();

      images = new ArrayList<Drawable>();
     }

     public void addImage(Drawable d) {
      images.add(d);
     }

     public boolean isEmpty() {
      return getCount() == 0;
     }

     public int getCount() {
      return images.size();
     }

     public Drawable getItem(int position) {
      return images.get(position);
     }

     public long getItemId(int position) {
      return position;
     }

     public View getView(int position, View contentView, ViewGroup parent) {
      ImageView i = new ImageView(mContext);
      i.setImageDrawable(images.get(position));

      i.setLayoutParams(new Gallery.LayoutParams(160, 120));
      i.setScaleType(ImageView.ScaleType.FIT_XY);
      i.setBackgroundResource(mGalleryItemBackground);

      return i;
     }
}

When the view is to be displayed with an empty ArrayList, getCount() does get called (returning 0), but the Gallery never checks isEmpty, and when I had defined getEmptyView() in the Gallery, it was never called either. Did I miss another required method in BaseAdapter to properly notify the empty state?

Thanks!

A: 

With the assistance of this article, I found the answer:

http://stackoverflow.com/questions/2807253/correct-use-of-setemtpyview-in-adapterview

The key to the issue was that (once I got the Gallery/AdapterView to properly call the empty status check using the addendum information) AdapterView is designed only to switch the View visibility settings between the content and empty views (swapping View.GONE and View.VISIBLE). Therefore, if you didn't do the legwork of properly creating and laying out both the content and empty views in the parent layout, they will not display properly.

In may case, I had created the empty view programmatically (just a TextView) and used setEmptyView() to attach it to the adapter view. The TextView was never attached to the LinearLayout that represented the Activity, so it didn't show up even after the AdapterView so kindly set it View.VISIBLE.

Wireless Designs