Click here to Skip to main content
15,886,362 members
Articles / Mobile Apps / Android

Sectioned ListView for Android Using Mono C#

Rate me:
Please Sign up or sign in to vote.
4.33/5 (8 votes)
14 Mar 2013CPOL6 min read 39.6K   1.2K   23  
Develop a Preferences style sectioned list adapter using Android Mono C#.
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();
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions