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, 1000);
    IEnumerable<string> strings = numbers.Select(
      i => string.Format("Item Number {0} Here!", i));
    List<string> data = new List<string>(strings);
  4. We then create an IListAdapter instance to connect the data to the view:
    ArrayAdapter<string> adapter = new ArrayAdapter<string>(
      this, Android.Resource.Layout.SimpleListItem1, data);
  5. We can then assign the adapter to the listView instance:
    listView.Adapter = adapter;
  6. Finally, if we want to handle item selections, we attach the following code to the ItemClick event:
    listView.ItemClick += (sender, e) => {
      using (var dialog = new AlertDialog.Builder(this)) {
        int position = e.Position;
        string value = data[position];
        dialog.SetTitle("Item Selection");
        dialog.SetMessage(value);
        dialog.Show();
      }
    };

How it works...

Presenting collections of data to a user is very common and is easily implemented using a ListView type. There are a few other types of views that we can use to display data, such as the Spinner and GridView type, but these are just variations of the list view.

The ListView type presents the data by applying a particular item template to each data item. The data is rendered onto the view or collection of views and then displayed in a scrollable list. The list view has many available customizations and can be styled to a great degree.

The list view makes use of adapter types that implement the IListAdapter interface, which maps the data to the ListView items. The adapter takes each item in the data collection and creates an item for the list view based on the provided template. In the case of our example, we use a simple adapter, the ArrayAdapter<T> type, which takes a string data item and renders it using the SimpleListItem1 item template, which happens to be a single TextView element.

If we want to be able to handle item selections, we can subscribe to the ItemClick event. This event provides an ItemClickEventArgs, which gives us access to several details of the selected item, including the actual list item view and the position of the item in the adapter.

There's more...

Lists are so common in mobile apps that there is a special class to display lists of data—the ListActivity type. This class is simply an activity with a list view built in, so we don't have to worry about a layout. To access the list view, we use the ListView property; to access the adapter, we use the ListAdapter property.

When we want to handle item selections, there are two ways we can go about this. We can either subscribe to the ItemClick event on the list view or we can override the OnItemClick() method of the ListActivity type.

See also

  • The Using custom ListView items recipe
  • The Using a BaseAdapter with arbitrary data recipe
  • The Enabling fast scrolling recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset