Skip to content

Instantly share code, notes, and snippets.

@zacg
Created August 18, 2013 16:42
Show Gist options
  • Save zacg/6262608 to your computer and use it in GitHub Desktop.
Save zacg/6262608 to your computer and use it in GitHub Desktop.
Simple Text Adapter For Xamarin ListViews
public class SimpleTextAdapter<T>
: BaseAdapter<T>
where T : new()
{
private T[] items;
private Activity context;
private Func<T,string> displayName;
private Func<T,long> itemId;
public SimpleTextAdapter(Activity context, T[] items, Func<T,string> displayName, Func<T,long> itemId)
: this(context, items,displayName) {
this.itemId = itemId;
}
public SimpleTextAdapter(Activity context, T[] items, Func<T,string> displayName)
: base() {
this.context = context;
this.items = items;
this.displayName = displayName;
}
public override long GetItemId(int position)
{
if (itemId != null)
return itemId (items[position]);
else
return position;
}
public override T this[int position] {
get { return items[position]; }
}
public override int Count {
get { return items.Length; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView; // re-use an existing view, if one is available
if (view == null) // otherwise create a new one
view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = displayName(items[position]);
return view;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment