Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Sectioned ListView for Android Using Mono C#

, 14 Mar 2013
Develop a Preferences style sectioned list adapter using Android Mono C#.
foodtypesectionlist_4.0.3.zip
FoodTypeSectionList
FoodTypeSectionList
Assets
bin
Release
FoodTypeSectionList.dll
FoodTypeSectionList.pdb
obj
Debug
android
assets
bin
classes
foodtypesectionlist
Activity1.class
foodtypesectionlist
R$attr.class
R$drawable.class
R$id.class
R$layout.class
R$string.class
R.class
FruitTypeListAdapter.class
JavaObjectHandler.class
MeatTypeListAdapter.class
SectionedListAdapter.class
VegetableTypeListAdapter.class
mono
android
Seppuku.class
MonoPackageManager.class
MonoPackageManager_Resources.class
MonoRuntimeProvider.class
classes.dex
FoodTypeSectionList.FoodTypeSectionList.apk
mono.android.jar
packaged_resources
resources_and_assemblies
foodtypesectionlist
foodtypesectionlist
src
foodtypesectionlist
foodtypesectionlist
mono
android
DesignTimeResolveAssemblyReferencesInput.cache
FoodTypeSectionList.dll
FoodTypeSectionList.pdb
R.cs.flag
res
drawable
icon.png
layout
main.axml
values
TempPE
__library_projects__
Release
android
assets
FoodTypeSectionList.dll
Mono.Android.dll
Mono.Android.dll.mdb
Mono.Security.dll
Mono.Security.dll.mdb
mscorlib.dll
mscorlib.dll.mdb
System.Core.dll
System.Core.dll.mdb
System.dll
System.dll.mdb
bin
classes
foodtypesectionlist
Activity1.class
foodtypesectionlist
R$attr.class
R$drawable.class
R$id.class
R$layout.class
R$string.class
R.class
FruitTypeListAdapter.class
JavaObjectHandler.class
MeatTypeListAdapter.class
SectionedListAdapter.class
VegetableTypeListAdapter.class
mono
MonoPackageManager.class
MonoPackageManager_Resources.class
MonoRuntimeProvider.class
classes.dex
mono.android.jar
packaged_resources
foodtypesectionlist
foodtypesectionlist
src
foodtypesectionlist
foodtypesectionlist
mono
assemblies
FoodTypeSectionList.dll
Mono.Android.dll
Mono.Android.dll.mdb
Mono.Security.dll
Mono.Security.dll.mdb
mscorlib.dll
mscorlib.dll.mdb
System.Core.dll
System.Core.dll.mdb
System.dll
System.dll.mdb
System.Xml.dll
System.Xml.dll.mdb
DesignTimeResolveAssemblyReferencesInput.cache
FoodTypeSectionList.dll
FoodTypeSectionList.pdb
link.flag
R.cs.flag
res
drawable
icon.png
layout
main.axml
values
TempPE
__library_projects__
Properties
Resources
Drawable
Icon.png
Layout
Main.axml
Values
FoodTypeSectionList.suo
FoodTypeSectionList_4_0_3_clean-noexe.zip
Icon.png
Main.axml
FoodTypeSectionList_4_0_3_clean.zip
FoodTypeSectionList.dll
Icon.png
Main.axml
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)

About the Author

stebo0728

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140718.1 | Last Updated 14 Mar 2013
Article Copyright 2012 by stebo0728
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid