3

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!

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Addendum: I found the setEmptyView(View emptyView) method (oops), which has allowed me to set a view on my Gallery that should display with an empty ArrayList, and now the empty status of the Adapter IS getting checked, but the view is still not displaying! Any ideas or thoughts? – devunwired Aug 08 '10 at 12:07

1 Answers1

1

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

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.

Community
  • 1
  • 1
devunwired
  • 62,780
  • 12
  • 127
  • 139
  • to expand - 2 tricks I've noticed with this: 1) make sure you have created & added the view to the parent of the list view; 2) make sure your adapter's `isEmpty()` method is correctly returning `true`. – Richard Le Mesurier Nov 08 '11 at 09:08