Android Adapter

| Page Views: 1682

Adapter is an interface defined in the android.widget package that acts as a bridge between the AdapterView and the underlying data for that view.

An adapter has two responsibilities:

  1. Provide access to data items.
  2. Make View objects for each item in the data set.

Here are the main implementors of the the Adapter interface:

  1. BaseAdapter.
  2. ArrayAdapter.
  3. SimpleCursorAdapter.
  4. CursorAdapter.
  5. SimpleAdapter etc.

Commonly used Adapter Methods

This interface has some method signatures that will typically be implemented by concrete classes implementing this interface. Some of them include:

(a). int getCount()

This method returns the total number of items in the data set that this adapter represents.

(b). Object getItem(int position) :

This method will retrieve the data item that's associated with the specified position in the data set.

As a parameter it takes the position of the item whose data we want within the adapter's data set. It will return the an Object which is the data at the specified position.

(c). long getItemId(int position

This method will retrieve the row id that's associated with the specified position in the list.It will take as a parameter the position of the item within the adapter's data set whose row id we want. It will return the id of the item at the specified position.

(c). View getView(int position,View convertView,ViewGroup parent)

This method as you cans ee returns an android.view.View object. This returned View object will be responsible for displaying data.

This is the data whose position is specified by the integer passed as a parameter in this method.There are two ways of creating this View.

First manually by instantiating.

Secondly by Inflation from a Layout resource(most common). After the inflation, the adapterview like ListView and GridView will apply the default parameters. Now this happens only if did not use the inflate(int,ViewGroup,boolean) method of the android.view.LayoutInflater class to specify the root view and prevent attachment to the root. There are three parameters passed.

The position represents the position of the item within the adapter's data set. The convertView. This represents the already inflated View object that we want to reuse. You want to check that this View actually exists by checking if it's not null before using. If it is then you inflate it. It's also possible to create new views for example that can be used by heterogeneous lists. The parent is basically a ViewGroup onto which this view will be attached to.

Types of Adapters

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

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

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.

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

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.

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


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