Android UI 设计:ListView 控件与 SimpleAdapter 适配器

SimpleAdapter


SimpleAdapter也是Android自己提供的一个Adapter适配器,它与ArrayAdapter不同的是ArrayAdapter需要使用Android自己定义的view布局文件,而SimpleAdapter则可以使用我们自己定义的布局文件。要学习SimpleAdapter的使用首先然我们看一下SimpleAdapter的构造器:




从图片中我们可以看出,SimpleAdapter只有一个构造器:


  • 第一个参数Context context是指当前的Activity,我们传入this即可。


  • 第二个参数List<? extends Map<String, ?>>是指传入的数据类型必须是List集合,集合存放的数据类型必须是Map。


  • 第三个参数int resource是指View的布局文件。也就是用来显示数据的View。


  • 第四个参数 String[] from数据是以Map类型存放在List集合中的,from参数是指存放在List中每条Map数据的键值集合。


  • 第五个参数int[] to是指将每条Map类型的数据中的不同键值对应到不同的得布局控件中。介绍完构造器的参数,大家可能还是不太懂到底是应该如何使用,没关系,我们通过例子来说明使用。


SimpleAdapter的使用


One.定义一个ListView的布局文件。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<ListView

android:id="@+id/listview_array"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:cacheColorHint="#00000000"

android:divider="#f00000"

android:dividerHeight="2dp">

</ListView>

</LinearLayout>


Two.书写一个View的布局文件,将数据以该View的形式存放在ListView中。


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_margin="20dp"

android:gravity="center|left"

android:background="@drawable/item_background">

<ImageView

android:id="@+id/image_photo"

android:layout_width="70dp"

android:layout_height="70dp" />

<TextView

android:id="@+id/textview_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="姓名"

android:textStyle="bold"

android:textColor="#0e99ff"

android:textSize="20sp" />

<LinearLayout

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical"

android:layout_marginRight="15dp"

android:layout_marginLeft="15dp">

<TextView

android:id="@+id/textview_age"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="性别"

android:textStyle="bold"

android:textColor="#009900"

android:textSize="15sp"/>

<TextView

android:id="@+id/textview_sex"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="年龄"

android:textStyle="bold"

android:textColor="#ff99ff"

android:textSize="15sp" />

</LinearLayout>

<TextView

android:id="@+id/textview_hobby"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="爱好"

android:textStyle="bold"

android:textColor="#55eedd"

android:textSize="20sp" />

</LinearLayout>


Three.创建数据。创建List的集合存放Map类型的数据,并对其进行初始化。(这些直接在Activity中定义)


public HashMap<String, String> createHashMap(String name, String age, String sex, String hobby){

HashMap<String, String> map = new HashMap<String, String>();

map.put("name", name);

map.put("age", age);

map.put("sex",sex);

map.put("hobby", hobby);

return map;

}

public void initList(){

mData = new ArrayList<HashMap<String, String>>();

HashMap zhangsan = createHashMap("张三", "18", "男", "喜欢打篮球,睡觉");

mData.add(zhangsan);

HashMap lisi = createHashMap("李四", "19", "女", "喜欢看书");

mData.add(lisi);

HashMap wangwu = createHashMap("王五", "22", "男", "喜欢玩游戏");

mData.add(wangwu);

HashMap zhaoliu = createHashMap("赵六", "21", "男", "喜欢吃饭,睡觉");

mData.add(zhaoliu);

}


Four.在onCreate()方法中初始化数据,创建SimpleAdapter的对象


String[] from = new String[]{"name", "age", "sex", "hobby"};

int[] to = new int[]{R.id.textview_name,R.id.textview_age,R.id.textview_sex,R.id.textview_hobby};

initList();

SimpleAdapter simpleAdapter = new SimpleAdapter(this,mData,R.layout.item_simpleadapter,from,to);


Five.调用setAdapter()方法,将View添加到ListView中。


mListViewArray.setAdapter(simpleAdapter);


Activity整体的代码为:


public class ListActivity extends Activity {

private ListView mListViewArray;

//定义数据。

private List<HashMap<String, String >> mData;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_list);

//创建ListView的对象。

mListViewArray = (ListView) findViewById(R.id.listview_array);

//初始化数据,通过调用自己定义的初始化方法。

initList();

//创建SimpleAdapter对象,将添加到数据添加到自己定义的View中。

SimpleAdapter simpleAdapter = new SimpleAdapter(this,mData,R.layout.item_simpleadapter,

new String[]{"name", "age", "sex", "hobby"},new int[]{R.id.textview_name,R.id.textview_age,R.id.textview_sex,R.id.textview_hobby});

//将View添加到ListView中。

mListViewArray.setAdapter(simpleAdapter);

}

//定义Map类型的数据。

public HashMap<String, String> createHashMap(String name, String age, String sex, String hobby){

HashMap<String, String> map = new HashMap<String, String>();

map.put("name", name);

map.put("age", age);

map.put("sex",sex);

map.put("hobby", hobby);

return map;

}

//对数据进行初始化的方法。

public void initList(){

mData = new ArrayList<HashMap<String, String>>();

HashMap zhangsan = createHashMap("张三", "18", "男", "喜欢打篮球,睡觉");

mData.add(zhangsan);

HashMap lisi = createHashMap("李四", "19", "女", "喜欢看书");

mData.add(lisi);

HashMap wangwu = createHashMap("王五", "22", "男", "喜欢玩游戏");

mData.add(wangwu);

HashMap zhaoliu = createHashMap("赵六", "21", "男", "喜欢吃饭,睡觉");

mData.add(zhaoliu);

}

}


结果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值