Android Baseadapter Tutorial With Example | getview viewholder

I will describe Android Baseadapter Tutorial With Example in this article.

Base adapter is the core class to make various adapters like array adapter, simple adapter or cursor adapter.

If you do not know what is the adapter in android then visit android adapter tutorial.

Basically, Adapter is the bridge between the data source and a final views like listview, gridview or spinner.

Adapter will retrieve the data from the data source and will transfer this data to the UI widgets like istview, gridview or spinner.

We can compare adapter with electrical adapter. Mobile device which need electricity is listview while electricity is a data source.

Adapter will provide the electricity (Data) to the mobile device (Listview, GridView, Spinner).

Basics about Android BaseAdapter

Base adapter is the main source of classes provided by android built in system to make any other custom adapters.

For example, when you want to make custom adapter with your custom requirements you need to extend the Base adapter.

Base adapter will provide the core methods, which we can override with our custom code and functions.

ListView, GridView or Spinner with complex structure will require custom version of the base adapter.

By extending the Base Adapter, you can create any kind of custom adapter that is required in Listview, GridView or spinner implementation.


Following is the sample code of any custom adapter who extends the Base adapter

As above code says that custom adapter is extending the base adapter and overriding the necessary methods.

Let us now understand the working of the methods of the Base Adapter.

1. areAllItemsEnabled()

Indicates whether all the items in this adapter are enabled. If the value returned by this method changes over time, there is no guarantee it will take effect. If true, it means all items are selectable and clickable (there is no separator.)

2. getItemViewType(int position)

Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.

ListView can be have more than one type of row (For example, header row, sub header row, child row etc.) . This method will return which type of row it is.

Code example can be like the below

3. getViewTypeCount()

Returns the number of types of Views that will be created by getView(int, View, ViewGroup).

It will return the total number of view types in the list. If listview have header row, sub header row, child item and sub child item than this method will return 4.

You can write this method as the following

4. getCount()

How many items are in the data set represented by this Adapter.

This method will return the total number of row in the listview.

For grid view it will return the total number of images present in it.

In spinner, it will return the total number of spinner drop down options.

Following line says how this method looks a like

5. getItem(int position)

Get the data item associated with the specified position in the data set.

For example, If the data set is arraylist of string and position is 5th, then this method will return the string available at 5th position of arraylist.

6. getItemId(int position)

Get the row id associated with the specified position in the list.

7. getView(int i, View view, ViewGroup viewGroup)

This method will return the final view that a listview will set to it’s row’s ith (first parameter) position.

If listview has six rows then compiler will this method for six times.

In every iteration, compiler will fetch or inflate the XML file which will provide UI widgets (Textview, Button, ImageView etc.) for the row item.

Then it will set the appropriate values to the UI widgets (Textview, Button, ImageView etc.)

8. public CharSequence[] getAutofillOptions ()

Gets a string representation of the adapter data that can help AutofillService autofill the view backed by the adapter.

9. public View getDropDownView (int position, View convertView, ViewGroup parent)

Gets a View that displays in the drop down popup the data at the specified position in the data set.

This method is mostly used in the spinner implementation.

10.public void notifyDataSetChanged ()

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

When you change value of any UI widget like checkbox or any other, you should call this method to tell compiler that the data is changed.

11. public void notifyDataSetInvalidated ()

Notifies the attached observers that the underlying data is no longer valid or available. Once invoked this adapter is no longer valid and should not report further data set changes.

Base Adapter Example

Step 1. XML files

Make a new project in android studio.

Now in the activity_main.xml file, add the below source code

Just one listview in the main layout.

Make a new XML layout resource file and give it a name lv_item.xml

Write down the below code in the lv_item.xml

This file is the core view file for the listview.

Adapter will use this file to generate the layout of the each cell of the listview.

I have taken one textview and imageview in this file.

Click the below link to download images.

download demonuts_images_listview

After downloading the images, copy them into the res->drawable directory.

Step 2. Model and Images

Now make a new JAVA class and name it as

Code block for is as the following

This file has main two variable.

One is string variable and other one is integer, which will hold the reference of the images present in the drawable folder.

Getter and setter methods for both of this variables are also there.

Step 3. Adapter and Main classes

Now we will create the adapter class. Prepare a new JAVA class called

Copy the following code in

You can see that I have made a custom adapter by extending the base adapter.

I have also override the methods of base adapter like getCount(), getView(), getItem(), getItemId() etc.

Finally, code block for is looking like the below

Here, I have created the object of the CustomeAdapter class.

After attaching this object with listview, we will be able to populate our listview.

After this much work, when you run the app, you should get the below output

android baseadapter

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *