Xamarin.Android 入門之:Listview和adapter

來源:互聯網
上載者:User

標籤:

一、引言

不管開發什麼軟體,列表的使用是必不可少的,而本章我們將學習如何使用Xamarin去實現它,以及如何使用自訂配接器。關於xamarin中listview的基礎和適配器可以查看官網https://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/ 本章主要介紹如果在listview一項中顯示多條資料。

二、準備工作

1.建立一個Android項目取名為MyListview

2.建立2個視圖檔案

3.下面編輯我們的試圖:

將下列代碼複製並替換到StuAdapter中

 

<?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">    <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="30.5dp"        android:id="@+id/linearLayout1">        <TextView            android:text="姓名:"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:id="@+id/textView1" />        <TextView            android:text="Text"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:id="@+id/name" />    </LinearLayout>    <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/linearLayout2">        <TextView            android:text="年齡:"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:id="@+id/textView3" />        <TextView            android:text="Text"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:id="@+id/age" />    </LinearLayout></LinearLayout>
View Code

 

將下列代碼複製並替換到StudentList中

 

<?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/StList" /></LinearLayout>
View Code

 

4.建立一個活動取名為StudentsList,建立2個類分別取名為Info和StuAdapter,下面就可以開始編碼了。

三、綁定資料

1.首先我們需要一個容器來存放我們需要的資料,於是我們在Info中添加如下代碼

   public class Info:Java.Lang.Object    {        public string Name { get; set; }//定義學生姓名        public int Age { get; set; }//定義學生年齡    }
View Code

2.有了容器,我們現在就需要把我們的資料繫結綁定到適配器,於是我們自訂一個適配器,添加以下代碼

 public class StuAdapter : BaseAdapter    {        List<Info> StuItems;        Activity Con;        private LayoutInflater mInflater;        public StuAdapter(Activity context,List<Info> items)        {            this.StuItems = items;//擷取傳過來的資料            this.Con = context;            this.mInflater = LayoutInflater.From(context);        }        public override int Count        {            get            {                return StuItems.Count;            }        }        public override Java.Lang.Object GetItem(int position)        {            return StuItems[position];        }        public override long GetItemId(int position)        {            return 0;        }        /// <summary>        /// 最核心的方法        /// </summary>        /// <param name="position"></param>        /// <param name="convertView"></param>        /// <param name="parent"></param>        /// <returns></returns>        public override View GetView(int position, View convertView, ViewGroup parent)        {            StuView stuview;            if (convertView ==null) {                stuview = new StuView();                //我們要適配的控制項所在的視圖                convertView = mInflater.Inflate(Resource.Layout.StuAdapter, null);                //繫結控制項                stuview.name = convertView.FindViewById<TextView>(Resource.Id.name);                stuview.age = convertView.FindViewById<TextView>(Resource.Id.age);                //設定控制項要顯示的文字                stuview.name.Text = StuItems[position].Name;                stuview.age.Text = StuItems[position].Age.ToString();                convertView.Tag = stuview;            }            else            {                stuview = (StuView)convertView.Tag;            }            return convertView;        }        //定義一個容器來存放控制項,注意要繼承javaobject        public class StuView:Java.Lang.Object        {            public TextView name;            public TextView age;        }    }
View Code

3.下面我們就可以繫結資料行表了,將下列代碼添加到StudentsList活動中

 public class StudentsList : Activity    {        public List<Info> item;//定義一個列表        public ListView listview;//定義控制項        public StuAdapter adapter;//定義資料來源        protected override void OnCreate(Bundle savedInstanceState)        {            base.OnCreate(savedInstanceState);            SetContentView(Resource.Layout.StudentsList);//設定要顯示的視圖            AddDate();            listview = FindViewById<ListView>(Resource.Id.StList);//找到控制項            adapter = new StuAdapter(this, item);//綁定適配器            listview.Adapter = adapter;//設定listview的資料來源為adapter        }        /// <summary>        /// 添加資料        /// </summary>        public void AddDate()        {            //添加三條資料            Info info = new Info { Name = "張三", Age = 19 };            Info info1 = new Info { Name = "李四", Age = 30 };            Info info2 = new Info { Name = "王二", Age = 20 };            item = new List<Info>();            item.Add(info);            item.Add(info1);            item.Add(info2);        }    }
View Code

4.最後在我們的mainactivity活動中重寫button的點擊事件用來跳轉到StudentsList就ok了。

 Intent intent = new Intent(); intent.SetClass(this, typeof(StudentsList)); StartActivity(intent);

 5.

 

Xamarin.Android 入門之:Listview和adapter

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.