Displaying List Data in Xamarin.Android and Visual Studio

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








0 Comments

Featured Post

Improving C# Performance by Using AsSpan and Avoiding Substring

During development and everyday use, Substring is often the go-to choice for string manipulation. However, there are cases where Substring c...

MSDEVBUILD - English Channel

MSDEVBUILD - Tamil Channel

Popular Posts