0
点赞
收藏
分享

微信扫一扫

Android(二十三):Adapter之ArrayAdapter、SimpleAdapter


Adapter

Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带

基本ListView

Android(二十三):Adapter之ArrayAdapter、SimpleAdapter_xml

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/list"/>

<resources>
<string-array name="list">
<item>张三</item>
<item>李四</item>
<item>王五</item>
</string-array>
</resources>

ArrayAdapter

Android(二十三):Adapter之ArrayAdapter、SimpleAdapter_android_02

<ListView
android:id="@+id/list_view_01"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<!-- Resources/layout/list_item.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingBottom="5dp">

<ImageView
android:id="@+id/avatar"
android:layout_width="60dp"
android:layout_height="match_parent"
android:baselineAlignBottom="true"
android:src="@drawable/avatar"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp">

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#1D1D1C"
android:textSize="20sp"
android:text="ProsperLee"/>

<TextView
android:id="@+id/say"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#B4B4B9"
android:textSize="14sp"
android:text="prosper_lee" />

</LinearLayout>

</LinearLayout>

// ArrayAdapter
var listView01 = (ListView)FindViewById(Resource.Id.list_view_01);
var arr = new[] { "A", "B", "C" };
// 单独一行的文本框
var arrayAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, arr);
// // 两个文本框组成
// var arrayAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem2, arr);
// // 每项都是由一个已选中的列表项
// var arrayAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemChecked, arr);
// // 都带有一个复选框
// var arrayAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemMultipleChoice, arr);
// // 都带有一个单选钮
// var arrayAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemSingleChoice, arr);
if (listView01 != null) listView01.Adapter = arrayAdapter;

SimpleAdapter

Android(二十三):Adapter之ArrayAdapter、SimpleAdapter_android_03

<ListView
android:id="@+id/list_view_02"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

// SimpleAdapter
var listView02 = (ListView)FindViewById(Resource.Id.list_view_02);
IList<IDictionary<string, object>> list = new JavaList<IDictionary<string, object>>
{
new JavaDictionary<string, object>
{
{ "avatar", Resource.Drawable.avatar },
{ "name", "ProsperLee" },
{ "say", "人海茫茫,你我依旧孤独~~~" }
},
new JavaDictionary<string, object>
{
{ "avatar", Resource.Drawable.avatar },
{ "name", "Lee" },
{ "say", "谁又能来拯救我这灰暗的人生呢……" }
},
new JavaDictionary<string, object>
{
{ "avatar", Resource.Drawable.avatar },
{ "name", "You瞧谁不起" },
{ "say", "道不尽世间的沧桑,诉不完人生的悲凉!" }
}
};
var keys = new[] { "avatar", "name", "say" };
var targets = new[] { Resource.Id.avatar, Resource.Id.name, Resource.Id.say };
var simpleAdapter = new SimpleAdapter(this, list, Resource.Layout.list_item, keys, targets);
if (listView02 != null) listView02.Adapter = simpleAdapter;


举报

相关推荐

0 条评论