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

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 System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace FoodTypeSectionList
{
    public class SectionedListAdapter : BaseAdapter<ListSection>
    {
        private const int TYPE_SECTION_HEADER = 0;
        private Context _context;
        private List<ListSection> _sections;
        private LayoutInflater _inflater;
        public SectionedListAdapter(Context context) : base()
        {
            _context = context;
            _sections = new List<ListSection>();
            _inflater = LayoutInflater.From(_context);
        }
        public List<ListSection> Sections { get { return _sections; } set { _sections = value; } }
        public void AddSection(ListSection section) { _sections.Add(section); }
        public void AddSection(String caption, String columnHeader1, String columnHeader2, String columnHeader3, BaseAdapter adapter)
        {
            _sections.Add(new ListSection(caption, columnHeader1, columnHeader2, columnHeader3, adapter));
        }
        public override ListSection this[int index] { get { return _sections[index]; } }
        public override int Count
        {
            get
            {
                int count = 0;
                foreach (ListSection s in _sections) count += s.Adapter.Count + 1;
                return count;
            }
        }
        public override int ViewTypeCount
        {
            get
            {
                int viewTypeCount = 1;
                foreach (ListSection s in _sections) viewTypeCount += s.Adapter.ViewTypeCount;
                return viewTypeCount;
            }
        }
        public override long GetItemId(int position) { return position; }
        public override bool AreAllItemsEnabled() { return false; }
        public override int GetItemViewType(int position)
        {
            int typeOffset = TYPE_SECTION_HEADER + 1;
            foreach (ListSection s in _sections)
            {
                if (position == 0) return TYPE_SECTION_HEADER;
                int size = s.Adapter.Count + 1;
                if (position < size) return (typeOffset + s.Adapter.GetItemViewType(position - 1));
                position -= size;
                typeOffset += s.Adapter.ViewTypeCount;
            }
            return -1;
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView;
            foreach (ListSection s in _sections)
            {
                if (position == 0)
                {
                    if (view == null || !(view is LinearLayout)) view = _inflater.Inflate(Resource.Layout.ListSeparator, parent, false);
                    TextView caption = view.FindViewById<TextView>(Resource.Id.caption);
                    caption.Text = s.Caption;
                    TextView columnHeader1 = view.FindViewById<TextView>(Resource.Id.columnHeader1);
                    columnHeader1.Text = s.ColumnHeader1;
                    TextView columnHeader2 = view.FindViewById<TextView>(Resource.Id.columnHeader2);
                    columnHeader2.Text = s.ColumnHeader2;
                    TextView columnHeader3 = view.FindViewById<TextView>(Resource.Id.columnHeader3);
                    columnHeader3.Text = s.ColumnHeader3;
                    return view;
                }
                int size = s.Adapter.Count + 1;
                if (position < size) return s.Adapter.GetView(position - 1, convertView, parent);
                position -= size;
            }
            return null;
        }
        public override Java.Lang.Object GetItem(int position)
        {
            foreach (ListSection s in _sections)
            {
                if (position == 0) return null;
                int size = s.Adapter.Count + 1;
                if (position < size) return s.Adapter.GetItem(position - 1);
                position -= size;
            }
            return null;
        }
    }

    public class ListSection
    {
        private String _caption, _columnHeader1, _columnHeader2, _columnHeader3;
        BaseAdapter _adapter;
        public ListSection(String caption, String columnHeader1, String columnHeader2, String columnHeader3, BaseAdapter adapter)
        {
            _caption = caption;
            _columnHeader1 = columnHeader1;
            _columnHeader2 = columnHeader2;
            _columnHeader3 = columnHeader3;
            _adapter = adapter;
        }
        public String Caption { get { return _caption; } set { _caption = value; } }
        public String ColumnHeader1 { get { return _columnHeader1; } set { _columnHeader1 = value; } }
        public String ColumnHeader2 { get { return _columnHeader2; } set { _columnHeader2 = value; } }
        public String ColumnHeader3 { get { return _columnHeader3; } set { _columnHeader3 = value; } }
        public BaseAdapter Adapter { get { return _adapter; } set { _adapter = value; } }
    }
}

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)

Share

About the Author

stebo0728

United States United States
No Biography provided

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