In my previous post I talked about how to create SQLite databases in our Android.Xamarin applications. I wanted to show you how to do some more robust things in your apps with the power of databases but while I was creating that plan, I realized that while I had talked about how to use the ListView control in the past, I hadn’t really worked on how to do ListView adapters other than the simple ArrayAdapter class. Let’s remedy that right away!
Creating Custom ListAdapters
The basic ArrayAdapter class is great but if we really want to get some power behind our ListViews then we want to create a ListAdapter that will give us the ability to customize the view. When we use a ListAdapter we have to override the base class BaseAdapter and fill in the information on behavior and display that we want.
There are two properties that we must override in BaseAdapter:
- Count – To tell the control how many rows are in the data.
- this[int] indexer – To return the data associated with a particular row number.
There are two methods as well:
- GetView – To return a View for each row, populated with data. This method has a parameter for the
ListViewto pass in an existing, unused row for re-use.
- GetItemId – Return a row identifier (typically the row number, although it can be any long value that you like).
The most important part is the GetView method. GetView allows us to customize how we want to display each row inside of the ListView and also handles how rows are re-used in the View. When displaying hundreds or thousands of rows, however, it would be a waste of memory to create hundreds or thousands of
View objects when only eight fit on the screen at a time. To avoid this situation, when a row disappears from the screen its view is placed in a queue for re-use. As the user scrolls, the
GetView to request new views to display – if available it passes an unused view in the
convertView parameter. If this value is null then your code should create a new view instance, otherwise you can re-set the properties of that object and re-use it. Pretty cool, right?
A word of warning – if you don’t handle row re-use you will run the risk of your app chewing up all of the device memory and then crashing. Custom adapter implementations should always re-use the
convertView object before creating new views to ensure they do not run out of memory when displaying long lists.
A context menu is a type of pop-up menu that is attached to a particular view. In the example I am doing for you today, the menu pops up on top of the View and allows you to setup different menu items that you can then attach functionality to. In the case of the app I am building in this demonstration, we are managing the names and phone numbers of contacts we wish to store. If we should want to edit or delete those contacts, we can select them from our list and choose the appropriate action from the context menu.
In this video I will walk you through creating your custom ListAdapter and setup a Context Menu for your list. Stay tuned for Part 2 on this topic where we will use the context menu to call some SQLite database functionality.