Android SpinnerAdapter

| Page Views: 1481

SpinnerAdapter is an interface that binds android.widget.Spinner to its data.

This interface lives inside the android.widget package:

package android.widget;

Like the ListAdapter, this interface derives majority of its methods from the android.widget.Adapter interface.

public interface SpinnerAdapter extends Adapter {}

Spinners in android show data in dropdowns. They are the equivalence of ComboBox in say Swing or WinForms.

However,because of the SpinnerAdapter, spinners are very customizable.

This is because spinner adapter allows for definition of two different views:

  1. One showing the data in the spinner itself.

  2. One showing the data in a dropdown list when the spinner is pressed.

Methods in SpinnerAdapter

We said SpinnerAdapter inherits all its methods from the Adapter apart from the getDropDownView().

public View getDropDownView(int position,View convertView,ViewGroup parent): This method will return a android.view.View object that will display data at the specified position in the dropdown popup.

  • The position parameter is the index of the item whose view we want to return
  • The convertView is the old view we are recycling. Normally you check if it's null first before using it. You can also create a new View as well.
  • The parent parameter is the parent view onto which we will attach this View.

Classes that Implement SpinnerAdapter

SpinnerAdapter is an interface that's implemented by 6 classes and 1 interface. Some of classes are abstract but most concrete.

All these are defined inside the android.widget package:

  1. BaseAdapter - A super class of common implementations for an android.widget.Adapter interface.It implements both ListAdapter and SpinnerAdapter interfaces.

  2. ArrayAdapter - A BaseAdapter child which uses an array of arbitrary objects as data source.

  3. CursorAdapter - An abstract BaseAdapter child used to expose data from android.database.Cursor to a ListView.

  4. ResourceCursorAdapter - An abtract CursorAdapter child that provides an easy way to create views defined in an xml file.

  5. SimpleAdapter - An easy BaseAdapter child used to map static data to views defined in an XML file.

  6. SimpleCursorAdapter - A ResourceCursorAdapter child that provides an easy way to map columns from a cursor to TextViews or ImageViews defined in an XML file.

  7. ThemedSpinnerAdapter - This is an interface that provides extensions over SpinnerAdapter and is capable of inflating drop-down views againts a different theme than normal views.

Other Adapter Types

Let's look at some of the available adapter subclasses.

Android PagerAdapter Tutorial and Examples.

Normally you use PagerAdapters with ViewPager. ViewPager you know allows us swipe through pages.

Well PagerAdapter acts as the base class for providing the adapter to populate pages inside of that ViewPager.

BaseAdapter as the name suggests BaseAdapter is a base adapter, or super adapter. An adapter is a class that acts as a bridge between an adapterview and the underlying data source.

By being a base adapter BaseAdapter provides a common implementation for adapters that can be use...

ListAdapter is an interface that's used to bind a ListView to its data.

ArrayAdapter is concrete class that derives from the BaseAdapter and is backed by an array of arbitrary objects.

ArrayAdapter requires us to pass a single TextView as a resource as the layout. This is for simple layouts where you don't have to create a custom adapter.

For complex layouts...

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post