Sort Listview Alphabetically in Xamarin Android


In this post I want to share how easy it is to sort listview alphabetically in Xamarin Android using the awesome power of LINQ. The sample project for this post will be a fictitious employee directory listing employee’s names and departments. Here is what the finished app will look like this:

Project Creation
I will be creating the sample project using Visual Studio 2013, Update 4. You can accomplish the same using Xamarin Studio as well, however with Visual Studio you can take advantage of an excellent template contributed by James Montemagno that have AppCompat v7, Toolbar and Navigation Drawer setup and ready to go. If you want to follow along, please install James template following the standard Visual Studio extension. Do know that the template requires you to install the Android Support Library v7 either through Xamarin Components or Nuget. I just used Nugets. Here is what the project selection template looks like.


Project Structure
The name of the project is ProntoCorp. It has two objects: Employee and Department with the following properties


This project uses just one Activity MainActivity.cs and two Fragments: EmployeeList.cs and AddEditEmployee.cs. It uses a Custom Adapter EmployeeAdapter.cs to manage the listview and it is in the Adapter that I defined the method that sort the Adapter alphabetically. Here is the sort method in the listview:

The sort method could be defined any where but the reason I put it in the Adapter is so that the Adapter can just refresh itself after the sort. The sort method accepts enums that are defined in a Helper method like so:

To allow for the the selection of the sort, I hoked up a drop down list to the Toolbar menu with the following xml:

Data Persistence – SQLite-Net
I use the original SQLite-Net installed from Nuget. There is no need to install the SQLite.Net PCL since this is a standalone non-cross platform project. When you install this Nuget package it adds two files to your project: SQLite.cs and SQLiteAsync.cs. As you can tell from the names the files expose Synchronous and Asynchronous APIs. I used the Asynchronous API, even though this library does not provide native SQLite drivers for Android, using the Asynchronous API Android did not yell “Your application is doing too much on the thread ..” warning.


I used the support library Fragment instead of the App Fragment because features like the Navigation Drawer requires the support library. Make sure that in the Activity you define onOptionItemCreated and call the base implementation so it will allow the attached Fragments to participate in the handling of onOptionsItemsCreated. And in the onCreate methods of the Fragments, call HasOptionMenu = true – this was the runtime will know that your Fragments have some menu items to contribute to the menu bar. Did you notice the subtle API difference, in standard Fragment the API for this is SetHasOptionsMenu(true) in the support library it is HasOptionMenu = true like so:

When a sort type is selected in the Toolbar menu, here is how the Sort method in the adapter is called:

There you have it, fully functioning example of how to sort listview alphabetically in Xamarin Android. The full source code is available from Github.

About the Author valokafor

I am a Software Engineer with expertise in Android Development. I am available for Android development projects.

follow me on:

Leave a Comment:

Add Your Reply