Implementing a ListView

Almost all apps have some sort of collection of data that will be presented to the user, with the easiest and most common way being some sort of list.

How to do it...

If we want to present a collection of items to the user, we can use a ListView instance:

  1. In order to show a list, we need to add a ListView element to our layout resource (we can do this in the code as well):
    <ListView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/listView" />
  2. We then get hold of the ListView instance in our code so that we can use it:
    ListView listView = FindViewById<ListView>(
      Resource.Id.listView);
  3. Now that we have the view, we need the data (which we will generate here):
    IEnumerable<int> numbers = Enumerable.Range(1, ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.