Search

Xamarin Android ListViews - CustomAdapter

Monday 6 October 2014

ArrayAdapters are OK for showing a quick list but most of the time you'll need more power - you'll need to create a custom adapter. In this article we'll look at creating a custom adapter and layout, and using it in an activity. The source code for all of my list view posts is here.

Custom Adapters

A custom adapter lets you bind objects to a layout that you control. Here we'll display a list of cricketers. To do this we'll need...
  • A list of data.
  • A layout to display the items.
  • An adapter to bind the data to the layout.
  • A ListActivity to display the list.
The end result looks like this.

The Layout

The layout is a standard axml layout. In this case we'll show an imageview and a couple of textviews.

The Adapter

The adapter is a class which inherits from BaseAdapter. It takes a list of Player objects and binds them to the layout as required. This is all the code for the class.

This is all pretty simple - the most interesting method is GetView which is where the data is bound to the layout. Android will try to reuse existing layouts which is why the layout is only inflated when the view is null. Because the views can be reused you need to make sure all the controls are handled each time the method is called, otherwise the 'old' data will still be displayed.

The Activity

The activity is basically the same as in the simple example. We create a list of data, an adapter (a PlayerAdapter this time), and assign it to the ListAdapter property.

3 comments:

  1. Can you give me to see your class is Player.cs, thank

    ReplyDelete
  2. Casino - Bracket betting guide for your chance to win
    The Casino is a unique casino that has been around for over a casino-roll.com decade. It septcasino.com has managed to 토토 offer bsjeon great games such as Blackjack, Roulette and Video 메이저 토토 사이트 Poker,

    ReplyDelete