Android BaseAdapter

android baseadapter

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 used in:

  1. ListView,GridView - By use of listAdapter.
  2. Spinner - By use of spinnerAdapter.

BaseAdapter Details

  1. BaseAdapter was introduced back in Android API level 1.

  2. It's actually an abstract class in that it has some abstract methods that need overriding. Therefore you have to either choose to implement those methods or make your child class as well.
    public abstract class BaseAdapter ..{}
  3. BaseAdapter derives from java.lang.Object.
    public abstract class BaseAdapter extends Object ...{}
  4. BaseAdapter implements ListAdapter and SpinnerAdapter. SpinnerAdapter is used for spinners while ListAdapter for ListViews and GridViews.
    public abstract class BaseAdapter extends Object implements ListAdapter, SpinnerAdapter{..}

BaseAdapter Children and GrandChildren

BaseAdapter has several direct and indirect sub-classes.

No. Class Type Description
1. ArrayAdapter Direct While BaseAdapter is always abstract, this is a concrete implementation of BaseAdapter backed by an array of arbitrary objects.
2. SimpleAdapter Direct A BaseAdapter child we can use to provide mapping between static data to views defined in XML file.
3. CursorAdapter Direct A BaseAdapter child that exposes data from a cursor to a ListView.
4. ResourceCursorAdapter Indirect A CursorAdapter child that creates views defined in an XML file.
5. SimpleCursorAdapter Indirect A a ResourceCursorAdapter child that can map columns from Cursor or TextViews or ImageViews defined in an XML file.

Other Adapter Types

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

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.

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

This inteface resides inside the android.widget package:

package android.widget;

It derives from android.widget.Adapter, an interface that contains several signatures to be implemented by it's implementers.

public interface ListAdapter extends Adapter {}

Majority of the times that data needs to come from a Cursor object, however it's not mandatory. ListView can display any data as long as it's contained in a ListAdapter.

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 you c...

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.

Most of the time we don't use pageradapter directly but instead use it's implementations like:

  1. android.support.v4.app.FragmentPagerAdapter.
  2. android.support.v4.app.FragmentStatePagerAdapte.

There are 4 methods that are important to a PagerAdapter and have to be overriden:

  1. instantiateItem(ViewGroup,int)
  2. destroyItem(ViewGroup,int,Object)
  3. getCount()
  4. isViewFromObject(View,Object)

Best Regards.

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. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post