Day 19: ListView and Adapters, Part 2


This is part 2 of 2 part series exploring ListView and Adapters in Xamarin.Android in our 31 Days of Xamarin.Android Blog series.

In today’s post we will explore using BaseAdapter and also a custom layout for the ListView’s Item Row.

Let’s dive right into the code and look at what ListView’s AXML and Custom Item Row’s AXML would look like –

Gist file link:

Lets take a look at how the Main Activity that shows the ListView is –


Gist file link:

The main difference in this Activity vs what we covered yesterday was that we are no longer using ArrayAdapter but we now have our own custom MovieAdapter class, and the ListView’s Adapter is set to this Adapter.

So what exactly is in the MovieAdapter Class? Let’s explore –


Gist file link:

The MovieAdapter class which inherits from BaseAdapter mainly defines 4 methods that BaseAdapter abstract class mandates. They are –

  1. GetItemId(int position)
  2. GetView(int position, View convertView, ViewGroup parent)
  3. Count
  4. this[int position]


The GetItemId gives you an option to let ListView know what the id of the current item is looked up by position.


Count property is pretty straight forward, it tells us how many items the ListView is currently showing.

this[int position]

This is .NET’s array indexer overload method which exposes the object at a given position

GetView(int position, View convertView, ViewGroup parent)

The GetView method is THE most important method in an Adapter. The implementation of GetView starts with getting the View that it will be working with. All View’s in Android are inflated using LayoutInflater and GetView method is no different. Using the LayoutInflater we will identity which Layout, MovieRow in our context, to inflate.

Once we have the View that we will be working with inflated we can then use the regular FindViewById methods that we are already familiar with and set text, and possibly any other properties on the Views.

Let’s run the application now and see how it looks –


Ooh, pretty. This is just the beginning of what a custom ListView and Adapter can do and the opportunities on what you can do with it are endless.

Fast Scrolling

Fast Scrolling in a list view is extremely useful if there are lots of data that your ListView is showing. With fast scrolling you drag the scroll bar even more fast to get through lots of data.


To enable Fast Scroll, all you have to do is


Section Indexing

Now having fast scrolling is great, but what would make scrolling through lots of rows of data easy is something called Section Indexing. To enable Section Indexing, you will be inheriting your Activity from “ISectionIndexer

Lets see how the Adapter looks like after implementing methods mandated by ISectionIndexer –


Gist file link:

The Section Indexer methods that the activity should be implementing are –

  1. GetSections()
  2. GetPositionForSection()
  3. GetSectionForPosition()


Let’s Android know of all the Sections the Listview should be showing.


Gets the integer position for a given section.


Gets the section for a given position.

Now all these three methods make use of SectionIndexBuilder, a class I borrowed from Xamarin’s tutorials. Let’s see what SectionIndexBuilder does –


Gist file link:

All that SectionIndexBuilder does is some smart things with the List data to find out what sections and positions for a given list of data is.


Now, if you were to run the application, you should start seeing “sections” which makes it even more easier for your users to scroll through large lists of data.


That is a wrap with ListView and Adapters in Xamarin.Android, I’ll see you all tomorrow.

Get the CODE!
The following two tabs change content below.

Venkata Koppaka

Software Architect at Falafel Software
Venkata Koppaka has over 7 years experience developing variety of line of business applications to various financial and IT organizations. Venkata has been developing with all Telerik products since 2008, he ended working for Telerik on Sitefinity team as a senior developer for Ecommerce module. He is extremely interested in developing custom solutions for Sitefinity based Ecommerce stores. Venkata is a big fan of Test Driven Development and strives himself to write code that is completely testable and modular. Venkata has a Masters in Computer Science from Syracuse University, which drives his continuos learning addiction. In his spare time, Venkata develops apps for Windows Phone 8 platform and learns the most cutting edge technologies in software development. When he is not spending time on computer writing software, Venkata enjoys reading about cars and watching football.