Android in Mono开发初体验之ListAdapter

  本文给大家介绍在MonoDevelop环境下实现Android中的ListAdapter显示数据功能。

 

  前一段时间在网上看到可以使用C#开发Android的新闻,感觉非常不错。毕竟我也写过.NET程序,所以就特别好奇,特别想想看看如何使用C#来开发Android程序。然后就来到了Mono的官方网站下载安装程序并配置开发环境,至于如何配置其开发环境这里不再说述,有兴趣或需要的朋友直接登录Mono的官方网站查看:http://xamarin.com/

  目前MonoDevelop有收费和免费的版本,免费的只能在模拟器上运行,不能安装到真机中测试,不过这不影响我们研究MonoDevelop来开发Android程序。这个大家了解下就可以了。

 

  今天这个示例是使用ListAdapter来显示数据,我让ListAdapter直接继承自BaseAdapter。关于Adapter的详细信息在这里我不再详述,有兴趣的朋友直接上网搜索一下相关资料。接下来让我们先看一下今天这个示例的运行效果,如下图所示:

 

    

 

  下面呢,让我们在MonoDevelop中新建程序,选择Mono for Android Application模板,下面是程序结构图:

 

   

 

  我们需要在程序中新建一个类,命名为Application.cs,其代码如下:

using System;

namespace MyListAdapter
{
    /// <summary>
    /// Application.
    /// 应用程序实体类
    /// </summary>
    public class Application
    {
        public Application ()
        {
        }

        public string Name {
            get;
            set;
        }

        public string Description {
            get;
            set;
        }

        public int Image {
            get;
            set;
        }
    }
}

 

  接下来,我们需要在ListView中展示信息,那么就需要为ListView设置每行的Item样式。在MonoDevelop下设计布局界面和在Eclipse中差不多,代码也几乎一样。如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="80px">
    <ImageView
        android:id="@+id/imageView_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />
    <LinearLayout
        android:id="@+id/linearlayout_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_marginLeft="10px"
        android:layout_marginTop="10px">
        <TextView
            android:id="@+id/textview_top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
        <TextView
            android:id="@+id/textview_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>
</LinearLayout>

 

  不过,需要注意的是在引用布局中的View时,是使用类似于Resource.Id.imageView_item这样的方式。在ListAdapter类中,我让ListAdapter继承BaseAdapter,由于BaseAdapter是一个抽象类,这就要求我们实现其相应的属性和方法。看下面代码:

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 MyListAdapter
{
    /// <summary>
    /// List adapter.
    /// ListView数据适配器
    /// </summary>
    public class ListAdapter : BaseAdapter<Application>
    {
        private Activity context = null;
        public List<Application> list = null;

        /// <summary>
        /// 默认构造器
        /// </summary>
        public ListAdapter ()
        {
        }

        /// <summary>
        /// 带参构造器
        /// </summary>
        /// <param name="context"></param>
        /// <param name="list"></param>
        public ListAdapter (Activity context, List<Application> list): base()
        {
            this.context = context;
            this.list = list;
        }

        public override int Count {
            get { return this.list.Count; }
        }

        public override Application this [int position] {
            get { return this.list[position]; }
        }

        public override View GetView (int position, View convertView, ViewGroup parent)
        {
            var item = this.list[position];
            var view = convertView;

            if (convertView == null || !(convertView is LinearLayout))
            {
                view = context.LayoutInflater.Inflate(Resource.Layout.Main, parent, false);
            }

            var imageItem = view.FindViewById (Resource.Id.imageView_item) as ImageView;
            var tvName = view.FindViewById (Resource.Id.textview_top) as TextView;
            var tvDescrtion = view.FindViewById (Resource.Id.textview_bottom) as TextView;

            imageItem.SetImageResource (item.Image);
            tvName.SetText (item.Name, TextView.BufferType.Normal);
            tvDescrtion.SetText (item.Description, TextView.BufferType.Normal);

            view.Click += delegate(object sender, EventArgs e)
            {
                Toast.MakeText(context,((TextView)tvName).Text,ToastLength.Long).Show();
            };

            return view;
        }

        public override long GetItemId (int position)
        {
            return position;
        }
    }
}

 

  一般呢,我们会将ListAdapter作为ListView的数据桥梁,用ListView来展现数据。当然了,我们也可以不用ListView来达到展现数据的目的。打开MainActivity文件,将类继承的Activity修改成ListActivity,编写相应的代码:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;

namespace MyListAdapter
{
    [Activity (Label = "MyListAdapter", MainLauncher = true)]
    public class MainActivity : ListActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // 初始化列表数据
            InitListData();
        }

        /// <summary>
        /// 初始化列表数据
        /// </summary>
        public void InitListData()
        {
            this.ListAdapter = new ListAdapter(this,
            new List<Application>() {
                new Application() { Name = "angrybirds",Description = "angrybirds",Image = Resource.Drawable.angrybirds },
                new Application() { Name = "camera",Description = "camera",Image = Resource.Drawable.camera },
                new Application() { Name = "evernotealt",Description = "evernotealt",Image = Resource.Drawable.evernotealt },
                new Application() { Name = "fruitninja Mouse",Description = "fruitninja",Image = Resource.Drawable.fruitninja },
                new Application() { Name = "gtalk",Description = "gtalk",Image = Resource.Drawable.gtalk },
                new Application() { Name = "handcent",Description = "handcent",Image = Resource.Drawable.handcent },
                new Application() { Name = "maps",Description = "maps",Image = Resource.Drawable.maps },
                new Application() { Name = "tasks",Description = "tasks",Image = Resource.Drawable.tasks },
                new Application() { Name = "twitterblue",Description = "twitterblue",Image = Resource.Drawable.twitterblue },
                new Application() { Name = "widgetlocker",Description = "widgetlocker",Image = Resource.Drawable.widgetlocker },
                new Application() { Name = "youtube",Description = "youtube",Image = Resource.Drawable.youtube }, 
                new Application() { Name = "settingsa",Description = "settingsa",Image = Resource.Drawable.settingsa },
                new Application() { Name = "cam360",Description = "cam360",Image = Resource.Drawable.cam360 },
                new Application() { Name = "downloads",Description = "downloads",Image = Resource.Drawable.downloads },
                new Application() { Name = "dropbox",Description = "dropbox",Image = Resource.Drawable.dropbox },
                new Application() { Name = "evernote",Description = "evernote",Image = Resource.Drawable.evernote },
                new Application() { Name = "recorder",Description = "recorder",Image = Resource.Drawable.recorder }
            }
            );
        }
    }
}

 

  这样,相应的代码都已经编写完毕,在MonoDevelop中只需要按F5键运行就可以了,如果模拟器没启动,需要我们先动一个模拟器。另外,我们需要注意的是,如果大家是在VS2010中运行程序,需要点击"生成-->配置管理器",选择"布署",再点击运行就可以了。如下图:

 

   

 

  其实呢,使用MonoDevelop开发Android程序,只要我们有Android的开发经验,很快就能上手。即使是新手,有点C#基础上手起来也不是难事。以上就是在Mono中开发Android的ListAdapter并生成相应列表的过程。

  注意在运行示例时,一定要将Mono环境配置好。关于环境的配置不再详述。

 

  示例下载:/Files/hanyonglu/AndroidFile/MyListAdapter.rar

 
  Github地址:https://github.com/hanyonglu/MonoListAdapter

  最后,希望转载的朋友能够尊重作者的劳动成果,加上转载地址:http://www.cnblogs.com/hanyonglu/archive/2012/08/14/2638428.html 谢谢。

 

  完毕。^_^

posted @ 2012-08-14 17:42  Healtheon  阅读(16385)  评论(5编辑  收藏  举报