using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace FoodTypeSectionList
{
[Activity(Label = "FoodTypeSectionList", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
private List<MeatType> meats;
private List<VegetableType> vegetables;
private List<FruitType> fruits;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Populate our lists
List<MeatType> meats = new List<MeatType>();
meats.Add(new MeatType("Hamburger", "Ground chuck beef", 2.76));
meats.Add(new MeatType("Sirloin", "Sliced sirloin steaks", 4.56));
List<VegetableType> veggies = new List<VegetableType>();
veggies.Add(new VegetableType("Brocolli", "Cut brocolli floretes", 1.76));
veggies.Add(new VegetableType("Carrots", "Cut peeled baby carrots", 2.18));
List<FruitType> fruits = new List<FruitType>();
fruits.Add(new FruitType("Apple", "Granny smith apples", 0.87));
fruits.Add(new FruitType("Peach", "South Carolina peaches", 1.12));
// Now we create our adapters for the item types
MeatTypeListAdapter madptr = new MeatTypeListAdapter(this, Resource.Layout.FoodTypeListItem, meats);
VegetableTypeListAdapter vadptr = new VegetableTypeListAdapter(this, Resource.Layout.FoodTypeListItem, veggies);
FruitTypeListAdapter fadptr = new FruitTypeListAdapter(this, Resource.Layout.FoodTypeListItem, fruits);
// Now we create the sections and the sectioned adapter
SectionedListAdapter sadptr = new SectionedListAdapter(this);
sadptr.AddSection("Meats", "Name", "Description", "Price (lb.)", madptr);
sadptr.AddSection("Vegetables", "Name", "Description", "Price (lb.)", vadptr);
sadptr.AddSection("Fruits", "Name", "Description", "Price (lb.)", fadptr);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Fetch our list and populate it
ListView foodlist = FindViewById<ListView>(Resource.Id.foodlist);
foodlist.SetAdapter(sadptr);
foodlist.ItemClick += new EventHandler<AdapterView.ItemClickEventArgs>(foodlist_ItemClick);
}
private void foodlist_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
SectionedListAdapter adapter = (sender as ListView).Adapter as SectionedListAdapter;
JavaObjectHandler item = adapter.GetItem(e.Position) as JavaObjectHandler;
if (item != null)
{
if (item.Instance is MeatType)
{
Toast.MakeText(this, "You clicked on a meat: " + (item.Instance as MeatType).Name, ToastLength.Short).Show();
}
else if (item.Instance is VegetableType)
{
Toast.MakeText(this, "You clicked on a vegetable: " + (item.Instance as VegetableType).Name, ToastLength.Short).Show();
}
else if (item.Instance is FruitType)
{
Toast.MakeText(this, "You clicked on a fruit: " + (item.Instance as FruitType).Name, ToastLength.Short).Show();
}
}
}
}
}