Xamarin.Android Adapter初探

来源:互联网 发布:淘宝手机版海报尺寸 编辑:程序博客网 时间:2024/06/05 15:08

比较常用的有 Base Adapter,Impleader,Adapter,Counteradaptation等

  • BaseAdapter是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;

  • ArrayAdapter支持泛型操作,最为简单,只能展示一行字。

  • SimpleAdapter有最好的扩充性,可以自定义出各种效果。

  • SimpleCursorAdapter可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写其他方法。可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。

    应用案例

    列表的显示需要三个元素:

  • ListVeiw 用来展示列表的View。

  • 适配器 用来把数据映射到ListView上的中介。
  • 数据 具体的将被映射的字符串,图片,或者基本组件。
[Activity(Label = "AdapterTest", MainLauncher = true, Icon = "@drawable/icon")]    public class AdapterTest : Activity    {        private ListView listView;        protected override void OnCreate(Bundle bundle)        {            base.OnCreate(bundle);            // Create your application here            //string [] strs = { "1", "2", "3", "4", "5" };            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleExpandableListItem1, getData());            listView = new ListView(this);            listView.Adapter = adapter;            SetContentView(listView);        }        private List<String> getData()        {            List<String> data = new List<String>();            data.Add("测试数据1");            data.Add("测试数据2");            data.Add("测试数据3");            data.Add("测试数据4");            return data;        }    }

上面代码使用了Adapter(Context context, int resourcefulness, List objects)来装配数据,要装配这些数据就需要一个连接List View视图对象和数组数据的适配器来两者的适配工作,Adapter的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,Android.Resource.Layout.SimpleExpandableListItem1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时用adapter()完成适配的最后工作。效果图如下:
这里写图片描述

SimpleAdapter

simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。
案例一:
simple.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:id="@+id/img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="5dp"/><TextViewandroid:id="@+id/title"android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff"android:textSize="20sp"/></LinearLayout>

Activity

[Activity(Label = "SimpleAdapterActivity",MainLauncher=true)]    public class SimpleAdapterActivity : ListActivity    {        protected override void OnCreate(Bundle bundle)        {            base.OnCreate(bundle);            // Create your application here            ListAdapter = new SimpleAdapter((Context)this, getData(), Resource.Layout.simple,                new string[] { "title", "img" }, new int[] { Resource.Id.title, Resource.Id.img });        }        private IList<IDictionary<string, Object>> getData()        {            //map.put(参数名字,参数值)            IList<IDictionary<string, Object>> list = new JavaList<IDictionary<string, Object>>();            JavaDictionary<string, Object> map = new JavaDictionary<string, Object>();            map.Add("title", "张三");            map.Add("img",Resource.Drawable.Icon);            list.Add(map);            map = new JavaDictionary<string, Object>();            map.Add("title", "李四");            map.Add("img", Resource.Drawable.Icon);            list.Add(map);            map = new JavaDictionary<string, Object>();            map.Add("title", "王五");            map.Add("img", Resource.Drawable.Icon);            list.Add(map);            return list;        }      }

这里写图片描述

0 0