Using a SimpleAdapter

If we want more control over the parts of the data to be rendered and how the data is rendered for each item, we can use a different adapter and different item templates.

How to do it...

List views allow us to entirely customize each item view through item templates. Android has a few predefined templates that we can use:

  1. We can make use of the simple, single-line text items using the ArrayAdapter<T> type and a simple string collection:
    var adapter = new ArrayAdapter<string>(
      this, Android.Resource.Layout.SimpleListItem1, data);
  2. Android also provides a means to handle more complex data. To do this, we use a collection of the JavaDictionary<string, object> objects:
    var data = new JavaList<IDictionary<string, object>>();
  3. We can add items to this list like any other collection using the Add() method:
    data.Add(new JavaDictionary<string, object> {
      { "name", "Bruce Banner" },
      { "status", "Bruce Banner feels like SMASHING!" }
    });
  4. With more complex data, we need to use the SimpleAdapter type, which allows us to map a particular key in the dictionary to a view in the item:
    var adapter = new SimpleAdapter(
      this, data, Android.Resource.Layout.SimpleListItem1,
      new[] { "name" }, new[] { Android.Resource.Id.Text1 });
  5. The SimpleAdapter type also allows us to map multiple keys to multiple views in each item; to do this, we use a different item resource:
    SimpleAdapter adapter = new SimpleAdapter(
      this, data, Android.Resource.Layout.SimpleListItem2,
      new[] {
        "name",
        "status"
      }, new[] {
        Android.Resource.Id.Text1,
        Android.Resource.Id.Text2 
      });
  6. There are also items that support checkboxes, or some other means of showing that the item is selected:
    var adapter = new SimpleAdapter(
      this, data, Android.Resource.Layout.SimpleListItemChecked,
      new[] { "name" }, new[] { Android.Resource.Id.Text1 });
  7. In order to support selection, we have to ensure that the list view ChoiceMode is either Multiple or Single:
    listView.ChoiceMode = ChoiceMode.Single;

How it works...

Passing collections between .NET and Java is slightly more complex than it appears. When we pass a collection to Java, only a copy of the collection is passed. This means that, if the collection is modified by some object on the Java side, the changes are not reflected on the .NET side. Xamarin.Android has a way of addressing this problem by providing various Java collections, such as JavaList and JavaDictionary. These collections are simple wrappers to the underlying Java collections.

Note

When collections are passed to Java objects, a copy of the collection is created in Java, and that copy is used instead of the .NET collection.

There are advantages of using both types of collections. If we use the .NET collections, the objects exist in .NET and a copy is passed to Java. This copy is not linked to the original, and is managed separately by both the .NET and Java garbage collectors. The downside is that the .NET collection is not updated when the Java collection is; however, this method is not used very often.

If we use the Java collections, the collection is created in Java and a wrapper is created in .NET. This allows a collection to be updated if the other one is. However, every time we wish to access the items or update the collection, the item is passed to the underlying Java collection and back again.

Note

The .NET collections are useful when the operation is performed in .NET. The Java collections are useful if the data will be updated from the native Java.

The SimpleAdapter type allows us to easily map a slightly more complex datatype to a structured view. When constructing the adapter, we provide the data along with two arrays that are used to map an item property to a view.

The data is a collection of dictionaries, with each dictionary representing an item from the list. The dictionary is used to label a value. Each of the dictionaries should contain the same set of keys, as these represent the attributes of the data.

The last two parameters in the constructor, two arrays, are used together to map a dictionary key to a view ID. The first array contains the collection of dictionary keys, and the second contains a collection of IDs. The size of the arrays should be the same as each item corresponds directly to the item in the same position in the other array.

The adapter iterates through the items in the arrays and, after obtaining the value from the dictionary using the key in the first array, it updates the corresponding view using the ID in the second array.

There's more...

As the SimpleArray type takes a resource ID for the item template as a parameter, we can create our own item layouts and use those IDs instead of the Android ones. If we need to have even more control over the adapter, we can create our own adapter by inheriting from the BaseAdapter type.

See also

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

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