Created
August 18, 2013 16:42
-
-
Save zacg/6262608 to your computer and use it in GitHub Desktop.
Simple Text Adapter For Xamarin ListViews
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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