Xamarin.Android Adapter初探

比较常用的有 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"
>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
/>
<TextView
android: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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值