What is Adapter in Android?
adapter has two responsibilities:
- Provide access to data items.
Viewobjects for each item in the data set.
Here are the main implementors of the the
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:
This method returns the total number of items in the data set that this adapter represents.
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.
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.
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.
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.