Click here to Skip to main content
15,881,681 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.5K   1.2K   23  
Develop a Preferences style sectioned list adapter using Android Mono C#.
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 MeatTypeListAdapter : BaseAdapter<MeatType>
    {
        private Activity _context;
        private List<MeatType> _items;
        private int _templateResourceId;
        public MeatTypeListAdapter(Activity context, int templateResourceId, List<MeatType> items) : base()
        {
            _context = context;
            _templateResourceId = templateResourceId;
            _items = items;
        }
        public override MeatType this[int index] { get { return _items[index]; } }
        public override int Count { get { return _items.Count; } }
        public override long  GetItemId(int position) { return position; }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView;
            MeatType item = this[position];
            if (view == null || !(view is LinearLayout)) view = _context.LayoutInflater.Inflate(_templateResourceId, parent, false);
            TextView nameLabel = view.FindViewById<TextView>(Resource.Id.name);
            nameLabel.Text = item.Name;
            TextView descriptionLabel = view.FindViewById<TextView>(Resource.Id.description);
            descriptionLabel.Text = item.Description;
            TextView priceLabel = view.FindViewById<TextView>(Resource.Id.price);
            priceLabel.Text = item.PricePerPound.ToString("F2");
            return view;
        }
        public override Java.Lang.Object GetItem(int position)
        {
            if(position < _items.Count) return new JavaObjectHandler(_items[position]);
            return null;
        }
    }

    public class VegetableTypeListAdapter : BaseAdapter<VegetableType>
    {
        private Activity _context;
        private List<VegetableType> _items;
        private int _templateResourceId;
        public VegetableTypeListAdapter(Activity context, int templateResourceId, List<VegetableType> items) : base()
        {
            _context = context;
            _templateResourceId = templateResourceId;
            _items = items;
        }
        public override VegetableType this[int index] { get { return _items[index]; } }
        public override int Count { get { return _items.Count; } }
        public override long  GetItemId(int position) { return position; }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView;
            VegetableType item = this[position];
            if (view == null || !(view is LinearLayout)) view = _context.LayoutInflater.Inflate(_templateResourceId, parent, false);
            TextView nameLabel = view.FindViewById<TextView>(Resource.Id.name);
            nameLabel.Text = item.Name;
            TextView descriptionLabel = view.FindViewById<TextView>(Resource.Id.description);
            descriptionLabel.Text = item.Description;
            TextView priceLabel = view.FindViewById<TextView>(Resource.Id.price);
            priceLabel.Text = item.PricePerPound.ToString("F2");
            return view;
        }
        public override Java.Lang.Object GetItem(int position)
        {
            if (position < _items.Count) return new JavaObjectHandler(_items[position]);
            return null;
        }
    }

    public class FruitTypeListAdapter : BaseAdapter<FruitType>
    {
        private Activity _context;
        private List<FruitType> _items;
        private int _templateResourceId;
        public FruitTypeListAdapter(Activity context, int templateResourceId, List<FruitType> items) : base()
        {
            _context = context;
            _templateResourceId = templateResourceId;
            _items = items;
        }
        public override FruitType this[int index] { get { return _items[index]; } }
        public override int Count { get { return _items.Count; } }
        public override long  GetItemId(int position) { return position; }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView;
            FruitType item = this[position];
            if (view == null || !(view is LinearLayout)) view = _context.LayoutInflater.Inflate(_templateResourceId, parent, false);
            TextView nameLabel = view.FindViewById<TextView>(Resource.Id.name);
            nameLabel.Text = item.Name;
            TextView descriptionLabel = view.FindViewById<TextView>(Resource.Id.description);
            descriptionLabel.Text = item.Description;
            TextView priceLabel = view.FindViewById<TextView>(Resource.Id.price);
            priceLabel.Text = item.PricePerPound.ToString("F2");
            return view;
        }
        public override Java.Lang.Object GetItem(int position)
        {
            if (position < _items.Count) return new JavaObjectHandler(_items[position]);
            return null;
        }
    }
}

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