Skip to content

Instantly share code, notes, and snippets.

@Redth
Created October 12, 2010 23:19
Show Gist options
  • Save Redth/623108 to your computer and use it in GitHub Desktop.
Save Redth/623108 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics.Drawables;
namespace MonoDroid.CustomList
{
public class CustomListAdapter : BaseAdapter
{
Activity context;
public List<Animal> items;
public CustomListAdapter(Activity context) //We need a context to inflate our row view from
: base()
{
this.context = context;
//For demo purposes we hard code some data here
this.items = new List<Animal>() {
new Animal() { Name = "Elephant", Description = "Big and Gray, but what the hey", Image = Resource.drawable.elephant },
new Animal() { Name = "Chinchilla", Description = "Little people of the andes", Image = Resource.drawable.chinchilla },
new Animal() { Name = "Lion", Description = "Cowardly lion, anyone?", Image = Resource.drawable.lion },
new Animal() { Name = "Skunk", Description = "Ello, baby. I am ze locksmith of love, no?", Image = Resource.drawable.skunk },
new Animal() { Name = "Rhino", Description = "Most live to about 60, pretty old eh?", Image = Resource.drawable.rhino },
new Animal() { Name = "Zebra", Description = "Stripes maybe not so great for hiding", Image = Resource.drawable.zebra },
new Animal() { Name = "Squirrel", Description = "Nuts nuts, where's my nuts?!", Image = Resource.drawable.squirrel },
new Animal() { Name = "Walrus", Description = "I am he as you are he as you are me", Image = Resource.drawable.walrus },
new Animal() { Name = "Giraffe", Description = "Bigger than your ford pinto", Image = Resource.drawable.giraffe },
new Animal() { Name = "Chicken", Description = "I'll take 2 eggs over easy", Image = Resource.drawable.chicken },
new Animal() { Name = "Duck", Description = "He's all quacked up", Image = Resource.drawable.duck },
new Animal() { Name = "Hawk", Description = "He needs to be on a t-shirt", Image = Resource.drawable.hawk },
new Animal() { Name = "Lobster", Description = "We were at the beach...", Image = Resource.drawable.lobster },
new Animal() { Name = "Pig", Description = "Babe, Orson, Piglet, whatever", Image = Resource.drawable.pig },
new Animal() { Name = "Rabbit", Description = "Thumper is the best rabbit name ever", Image = Resource.drawable.rabbit },
new Animal() { Name = "Turtle", Description = "Slow and steady wins the race", Image = Resource.drawable.turtle },
};
}
public override int Count
{
get { return items.Count; }
}
public override Java.Lang.Object GetItem(int position)
{
return position;
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
//Get our object for this position
var item = items[position];
//Try to reuse convertView if it's not null, otherwise inflate it from our item layout
// This gives us some performance gains by not always inflating a new view
// This will sound familiar to MonoTouch developers with UITableViewCell.DequeueReusableCell()
var view = (convertView ??
context.LayoutInflater.Inflate(
Resource.layout.customlistitem,
parent,
false)) as LinearLayout;
//Find references to each subview in the list item's view
var imageItem = view.FindViewById(Resource.id.imageItem) as ImageView;
var textTop = view.FindViewById(Resource.id.textTop) as TextView;
var textBottom = view.FindViewById(Resource.id.textBottom) as TextView;
//Assign this item's values to the various subviews
imageItem.SetImageResource(item.Image);
textTop.SetText(item.Name, TextView.BufferType.Normal);
textBottom.SetText(item.Description, TextView.BufferType.Normal);
//Finally return the view
return view;
}
public Animal GetItemAtPosition(int position)
{
return items[position];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment