Visual Studio

Displaying List Data in Xamarin.Android and Visual Studio

By   Tuesday, July 19, 2016 comments
reads
React:

Introduction

Let we look regarding list view binding. The cross-platform applications with Xamarin.iOS, Xamarin.Android the ListView control binding is structurally similar.

Xamarin.IOS

UITableViewSource

Xamarin.Android

BaseAdapter

Your need follow Below steps for customizing a list view appearance


Step 1: Layout:

We need to create layout with List View controls.

HelloApp (ProjectName) Γ¨ Resources Γ¨ layout (Right Click) Γ¨ Add New Item Γ¨ Select ( Android layout ) Γ¨ Click Add

File Name: ListDemo.axaml and drag drop List View control into layout page

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/UserList" />
</LinearLayout>
****ListView Control name : UserList

Step 2: Model :

Create Model and add whatever property is required.

Model Name: User

Namespace :HelloApp.Model

namespace HelloApp.Model
{
public class User
{
public string UName;
}
}

Step 3: Adapter

Create adapter class and derived from BaseAdapter .

BaseAdapter is abstract class. we need implement following methods. We need select Row template and

namespace HelloApp.Adapters
{
class UserAdapters : BaseAdapter<User>
{
List<User> userlist;
Activity useractivity;
public UserAdapters(Activity context ,List<User> item):base()
{
useractivity = context;
userlist = item;
}
public override User this[int position]
{
get
{
throw new NotImplementedException();
}
}
public override int Count
{
get
{
return userlist.Count;
}
}
public override long GetItemId(int position)
{
return position;
}

public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = userlist[position];
if(convertView == null)
{
convertView = useractivity.LayoutInflater.Inflate(Android.Resource.Layout
SimpleExpandableListItem1,null);
}
convertView.FindViewById<TextView>(Android.Resource.Id.Text1).Text = item.uname;
return convertView;
}
}
}

Step 4: Activity

HelloApp (ProjectName) (Right Click) Γ¨ Add New Item Γ¨ Select ( Activity) Γ¨ Click Add

Source Code :

namespace HelloApp
{
[Activity(Label = "ListDemoActivity" , MainLauncher = true)]
public class ListDemoActivity : Activity
{
private ListView UserListView;
private List<User> userlist;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
base.SetContentView(Resource.Layout.ListDemo);
UserListView = FindViewById<ListView>(Resource.Id.UserList);
userlist = new List<User>();
userlist.Add(new User { UName = "Sutahahr" });
userlist.Add(new User { UName = "Suresh" });
userlist.Add(new User { UName = "Sumathi" });
userlist.Add(new User { UName = "Sujatha" });
UserListView.Adapter = new UserAdapters(this, userlist);
}
}
}

Source code explanation:

1. [Activity(Label = "ListDemoActivity (Title Of Text)" , MainLauncher = true(Initial launcher))]

2. private ListView UserListView – Find Control and assign to local variable

3. private List<User> userlist; - Assign Collection value

4. base.SetContentView(Resource.Layout.ListDemo); - Assign layout content in activity

5. UserListView = FindViewById<ListView>(Resource.Id.UserList); - Find user control from layout page

6. userlist = new List<User>(); - add collection

7. UserListView.Adapter = new UserAdapters(this, userlist); - Assign user collection into adapters








SJ
Suthahar Jegatheesan

Senior Solutions Architect · Microsoft Azure, AI & Mobile. Author of the Xamarin Q&A Book.

More in this series

No comments:

sponsored

What Colleagues Say

“Suthahar is one of the most thorough and knowledgeable cloud architects I’ve worked with. His depth on Azure and ability to make complex mobile solutions work cross-platform is remarkable. He always delivers beyond expectations.”

AT Arun T. Senior Engineering Manager · Direct Manager

“I’ve followed Suthahar’s MSDEVBUILD blog for years — practical, deep, always current. Working alongside him confirmed what I suspected: he’s as sharp in person as on paper, and a genuinely generous mentor and architect.”

RK Rajesh K. Lead Developer · Mindtree

Certifications & Recognition

AZ-300 · Azure Architect Technologies AZ-301 · Azure Architect Design AZ-104 · Azure Administrator Associate AI-100 · Designing Azure AI Solutions DP-900 · Azure Data Fundamentals MCPD · Windows Phone Certified SAFe® 4 Scrum Master

Technical Expertise

☁ Cloud Platforms Microsoft Azure — Expert · 18 yrs App Services · AKS · Functions · Cosmos DB · APIM · Entra ID AWS & Google Cloud — Advanced
πŸ€– AI & Automation Azure OpenAI / GenAI — Advanced · 5 yrs RAG Pipelines · Bot Framework · Cognitive Services · Vision & Speech AI · ML.NET
πŸ“± Mobile Architecture .NET MAUI & Xamarin — Expert · 12 yrs Flutter · Native iOS/Android · Offline-First · Secure Storage & Auth
πŸ”’ Identity & Security Microsoft Entra ID — Expert OAuth 2.0 · OIDC · Zero Trust · Conditional Access · RBAC · Key Vault

Speaking & Community

Microsoft User Group Malaysia Recurring speaker on Azure architecture, AI integration and .NET mobile development across Malaysia and APAC.
Global Azure Bootcamp — Chennai Presented “AI into Xamarin Mobile Apps” at GAB Chennai, part of the worldwide Global Azure community event.
AI & Azure OpenAI Community Events Workshops and live demos on RAG pipelines, AI agents and intelligent workflow automation since 2023.
Mobile & Cloud Workshops — Coimbatore Multi-day deep-dive sessions at GRG Computer Technology, KG Microsoft Innovation Center and RVS College of Arts and Science.
500+Blog Articles
50+Conference Talks
10K+Monthly Readers
30Public Repositories
3Continents Delivered
MSDEVBUILD Assistant Ask about Suthahar, Azure, AI or mentorship
Hi! I’m a simple FAQ bot for MSDEVBUILD. Tap a question below or type your own.