Xamarin ListView Example Android

In this tutorial you will learn how to create xamarin listview example android application using Xamarin IDE.

First i will give you some information about list view.

Xamarin List View

List View is most important UI component.It basically use to display list of data.It basically provides a simple scrolling functionality.A ListView instance requires an Adapter to feed it with data contained in row views.

Parts and Functionality of List View

  • Rows – The visible representation of the data in the list.
  • Adapter – A non-visual class that binds the data source to the list view.
  • Fast Scrolling – A handle that lets the user scroll the length of the list.
  • Section Index – A user interface element that floats over the scrolling rows to indicate where in the list the current rows are located.

There are some primary Classes which are basically used to display list view.

  • List View – user interface element that displays a scroll-able collection of rows. On phones it usually uses up the entire screen (in which case, the List Activity class can be used) or it could be part of a larger layout on phones or tablet devices.
  • View – a View in Android can be any user interface element, but in the context of a ListView it requires a View to be supplied for each row.
  • BaseAdapter – Base class for Adapter implementations to bind a ListView to a data source.
  • ArrayAdapter – Built-in Adapter class that binds an array of strings to a List View for display. The generic ArrayAdapter<T>does the same for other types.
  • CursorAdapter – Use CursorAdapter or SimpleCursorAdapter to display data based on an SQLite query.
  • This document contains simple examples that use an ArrayAdapter as well as more complex examples that require custom implementations of BaseAdapter or CursorAdapter.

So, let’s start by creating new android application project.File > New > Solution > Android App.Name it and hit Next > Create button.

Now no need to touch  your Main.axml file because i’ll create dynamically.So, go to your MainActivity.cs file.By default it will look like this.

namespace listview
{
	[Activity (Label = "listview", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity:Activity
	{
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
			
		}
	}
}

By default my MainActivity class is inherited with Activity class.According to this exercise i need ListActivity class.So, i am going to inherit ListActivity class instead of Activity.

namespace listview
{
	[Activity (Label = "listview", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity: ListActivity
	{
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
		}
	}
}

Next i will create a string array to consider them as a list view items and store some options.

namespace listview
{
	[Activity (Label = "listview", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity: ListActivity
	{
		string[] options;   // list item options array
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
			options = new string[]{"C#","Java","Python","PHP","C++","C","XML"};   // store values in array of options

		}
	}
}

Now i will bind array to list view using array adapter class and store in ListAdapter.

namespace listview
{
	[Activity (Label = "listview", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity: ListActivity
	{
		string[] options;   // list item options array
		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
			options = new string[]{"C#","Java","Python","PHP","C++","C","XML"};   // store values in array of options
			ListAdapter = new ArrayAdapter(this,Android.Resource.Layout.SimpleListItem1,options);
		}
	}
}

Now Run your android application project output will be.

listview,list view,Xamarin listview,xamarin listview example,xamarin listview example android,listview examle android,

that’s it for xamarin listview example android tutorial.This is a basic one in Part 2 i’ll illustrate everything regarding listview.I hope you understand everything.

For Java example Android Codec.

Thanks