[Xamarin] 簡單實作ListActivity

2013-07-15

之前寫到一篇文章透過StartActivityForResult傳值回來,透過跳到另外一個Activity 來選擇答案,之後透過intent(意圖) 回傳.

无命名xxx
但是文中案例因為是用事先設好的Layout 但是如果需要被選擇的東西很多時該怎麼辦
我們討論一下,如何製作很簡單的List .
首先我們得先參考一下再android 思維下要製作一個List 需要的架構圖
2013-07-15_125649

圖片參考: http://docs.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/part_1_-_listview_parts_and_functionality

很重要的一點 需要ListActivity 再來 需要指定其 ListAdapter ,不過基本上,因為如果只是簡單的列表,其android 有提供些比較簡單就可以實作出來的方法
首先我們直接開一個新的Activity  ,文中範例我命名為 ActivityTextList.cs
2013-07-15_113621

之後請注意雖然他預設繼承:Activity 但是我們將他改成繼承

:ListActivity



using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Widget;
 
namespace SimpleTextList
{
    [Activity(Label = "My Activity")]
    public class ActivityTextList :ListActivity
    {
        private string[] datas;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            //載入假資料
            datas = new string[] { "Apple", "Banana", "Cat", "Dracula", "Elf", "Fuxx", "Google", "Hadoop",
                "Internet", "Javascript", "Kungfu", "Launch", "Microsoft", "Nodejs", "Orange", "Programmer",
                "Queen", "Ricky", "Student", "Teacher", "Uniqlo", "Vicky", "Windows", "Xamarin", "Yeti", "Zebra" };
            this.ListAdapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleExpandableListItem1, datas);
 
 
        }
 
        //複寫點擊項目之後所做的動作
        //此案例為開啟一個新的Intent 且SetResult回傳 
        protected override void OnListItemClick(ListView l, View v, int position, long id)
        {
            //開一個新的 intent 傳回資料
            //詳細用法請參考: http://no2don.blogspot.com/2013/07/xamarin-startactivityforresult.html
            var intent = new Intent(this, typeof(Activity1));
            intent.PutExtra("name",datas[position]);
            SetResult(Result.Ok, intent);
            Finish();
 
        }
    }
}

請注意喔我是繼承 ListActivity 並非一般Activity 這時候我在使用版型中的 Android.Resource.Layout.SimpleExpandableListItem1 作為顯示
其中內建有四種可以選擇
2013-07-15_124732

其中 複寫 OnListItemClick 後可以自訂點選後的行為
我們來看看主Activity 的Code:


using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
 
namespace SimpleTextList
{
    [Activity(Label = "SimpleTextList", MainLauncher = true, Icon = "@drawable/icon")]
    public  class Activity1 : Activity
    {
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            SetContentView(Resource.Layout.Main);
 
 
            var btn1 = FindViewById<Button>(Resource.Id.btn1);
            btn1.Click += delegate
            {
                StartActivityForResult(typeof(ActivityTextList), 1);
            };
        }
 
        /// <summary>
        /// 複寫有Activity回傳結果的資料
        /// </summary>
        /// <param name="requestCode"></param>
        /// <param name="resultCode"></param>
        /// <param name="data"></param>
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
 
            if (requestCode == 1 && resultCode == Result.Ok)
            {
                Toast.MakeText(this, "選取結果(OnActivityResult):" + data.GetStringExtra("name"), ToastLength.Short).Show();
            }
        }
    }
 
 
}
 

結果:
Screenshot_2013-07-15-12-39-00

Screenshot_2013-07-15-12-39-03


文章參考:
http://docs.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/part_2_-_populating_a_listview_with_data
http://developer.android.com/reference/android/app/ListActivity.html

Source


當麻許的超技八 2014 | Donma Hsu Design.