Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
How to do custom layouts in Android with ListView
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using System.Linq;
namespace CustomLayoutTest
{
[Activity (Label = "CustomLayoutTest", MainLauncher = true)]
public class Activity1 : Activity
{
int count = 1;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
var items = new[] {
new NameWithCount { Name = "Hats", Count = 32 },
new NameWithCount { Name = "Economics Textbooks", Count = 17 },
new NameWithCount { Name = "Robots", Count = 110 },
};
var lv = FindViewById<ListView> (Resource.Id.listView1);
lv.Adapter = new MyCoolAdapter (items.ToList (), this);
}
}
public class MyCoolAdapter : BaseAdapter<NameWithCount>
{
Activity _parent;
List<NameWithCount> _items;
public MyCoolAdapter(List<NameWithCount> items, Activity parent)
{
_parent = parent;
_items = items;
}
public override View GetView (int position, View convertView, ViewGroup parent)
{
var view = _parent.LayoutInflater.Inflate (Resource.Layout.NameWithCountView, null);
var name = view.FindViewById<TextView> (Resource.Id.textView1);
var count = view.FindViewById<TextView> (Resource.Id.textView2);
name.Text = _items [position].Name;
count.Text = _items [position].Count.ToString();
return view;
}
public override int Count {
get {
return _items.Count;
}
}
public override NameWithCount this [int position] {
get {
return _items[position];
}
}
public override long GetItemId(int position)
{
return position;
}
}
public class NameWithCount
{
public string Name { get; set; }
public int Count { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.