Like

Welcome On Mobius

Mobius was created by professionnal coders and passionate people.

We made all the best only for you, to enjoy great features and design quality. Mobius was build in order to reach a pixel perfect layout.

Mobius includes exclusive features such as the Themeone Slider, Themeone Shorcode Generator and Mobius Grid Generator.

Our Skills

WordPress90%
Design/Graphics75%
HTML/CSS/jQuery100%
Support/Updates80%

Custom Android List View With Xamarin

By admin 2 years agoNo Comments

This blog shares the information of custom android list view with xamarin, here you will explore more about Base Adapter and also a custom layout for the ListView’s Item Row.

Let’s dive right into the code and look at what ListView’s AXML and Custom Item Row’s AXML would look like –

<?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/listView” />

</LinearLayout>

 <?xml version=”1.0″ encoding=”utf-8″?>

<TableLayout xmlns:android=”http://schemas.android.com/apk/res/android”

android:layout_width=”match_parent”

android:layout_height=”match_parent”>

<TableRow>

<ImageView

android:id=”@+id/img”

android:layout_width=”50dp”

android:layout_height=”50dp” />

<TextView

android:id=”@+id/txt”

android:layout_width=”wrap_content”

android:layout_height=”50dp” />

</TableRow>

</TableLayout>

 

Let’s take a look at how the Main Activity that shows the ListView is –

using Android.App;
using Android.Widget;
using Android.OS;

namespace XamarinDroidListView
{
[Activity(Label = “XamarinDroidListView”, MainLauncher = true, Icon = “@drawable/icon”)] public class MainActivity : Activity
{
ListView list;
string[] listitem = { ” C# Corner”, ” Xamarin”, ” Google Plus”, ” Twitter”, ” Windows”, ” Bing”, ” Itunes”, ” WordPress”, ” Drupal”, ” Whatapp” };
int[] imageId = {
Resource.Drawable.Icon,
Resource.Drawable.Icon,
Resource.Drawable.Icon,
Resource.Drawable.Icon,
Resource.Drawable.Icon,
Resource.Drawable.Icon,
Resource.Drawable.Icon,
Resource.Drawable.Icon,
Resource.Drawable.Icon,
Resource.Drawable.Icon
};
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
ServiceItemsAdapter adapter = new ServiceItemsAdapter(this, listitem, imageId);
list = (ListView)FindViewById(Resource.Id.listView);
list.Adapter = adapter;
list.ItemClick += List_ItemClick;
}

private void List_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
Toast.MakeText(this, “You Clicked at ” + listitem[e.Position], ToastLength.Long).Show();
}
}
}

The main difference in this Activity that we are no longer using ArrayAdapter but here we have our own custom ServiceItemsAdapter class, and the ListView’s Adapter is set to this Adapter.
So what exactly is in the ServiceItemsAdapter Class? Let’s explore –

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 XamarinDroidListView
{
public class ServiceItemsAdapter : BaseAdapter
{
private Activity context;
private string[] listitem;
private int[] imageId;
public override int Count
{
get
{
return listitem.Length;
}
}
public ServiceItemsAdapter(Activity context, string[] listitem, int[] imageId)
{
this.context = context;
this.listitem = listitem;
this.imageId = imageId;
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return listitem.Length;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = context.LayoutInflater.Inflate(Resource.Layout.CustomRow, parent, false);
TextView txtTitle = (TextView)view.FindViewById(Resource.Id.txt);
ImageView imageView = (ImageView)view.FindViewById(Resource.Id.img);
txtTitle.Text = (listitem[position]).ToString();
imageView.SetImageResource(imageId[position]);
return view;
}
}
}

The ServiceItemsAdapter class which inherits from BaseAdapter mainly defines 4 methods that BaseAdapter abstract class mandates. They are –

1. GetItemId(int position)
2. GetView(int position, View convertView, ViewGroup parent)
3. Count
4. GetItem(int position)

  • GetItemId:
    The GetItemId gives you an option to let ListView know what the id of the current item is looked up by position.
  • Count:
    Count property is pretty straight forward, it tells us how many items the ListView is currently showing.
  • GetItem[int position]:
    This is .NET’s array indexer overload method which exposes the object at a given position.
  • GetView(int position, View convertView, ViewGroup parent):
    The GetView method is THE most important method in an Adapter. The implementation of GetView starts with getting the View that it will be working with. All View’s in Android are inflated using LayoutInflater and GetView method is no different. Using the LayoutInflater we will identity which Layout, CustomRow in our context, to inflate.
    Once we have the View that we will be working with inflated we can then use the regular FindViewById methods that we are already familiar with and set text, and possibly any other properties on the Views.

Let’s run the application now and see how it looks –

 

Categories:
  Android Training, Blog, Technology
this post was shared 0 times
 000
About

 admin

  (204 articles)

Leave a Reply

Your email address will not be published.