为了账号安全,请及时绑定邮箱和手机立即绑定

Android零基础入门第41节:使用SimpleAdapter

标签:
Android

  通过ArrayAdapter实现Adapter虽然简单、易用,但ArrayAdapter的功能比较有限,它的每个列表项只能给一个TextView动态填充内容。如果开发者需要实现更复杂的列表项,则可以考虑使用 SimpleAdapter。

  一、使用SimpleAdapter

    这里需要注意的是,不要被SimpleAdapter的名字迷惑欺骗了,SimpleAdapter的功能不仅不简单,还十分强大,列表组件的大部分使用都是通过SimpleAdapter来提供列表项的。

    在使用SimpleAdapter之前,先来一起学习SimpleAdapter的构造方法,其构造方法如下:

SimpleAdapter(Contextcontext,List<?extendsMap<String,?>>data,intresource,String[]from,int[]to)

    从SimpleAdapter的构造方法可以看到,一共需要5个参数,这也是很多开发者觉得使用SimpleAdapter比较难的原因,其实就是没有很好的理解这5个参数。这个5个参数的含义如下:

·         context:要使用的上下文环境。

·         data:是一个List<? extends Map<String, ?>>类型的集合对象,该集合中每个Map<String, ?>对象生成一个列表项。

·         resource:界面布局文件的ID,对应的布局文件作为列表项的组件。

·         from:是一个String[]类型的参数,该参数决定提取Map<String, ?>对象中哪些key对应的value来生成列表项。

·         to:该参数是一个int[]类型的参数,该参数决定填充哪些组件。

二、示例

    接下来通过一个示例程序来学习如何使用SimpleAdapter创建ListView。

    继续使用WidgetSample工程的listviewsample模块,在app/main/res/layout/目录下创建simpleadapter_layout.xml文件,在其中填充如下代码片段:

[代码]xml代码:

?

01

02

03

04

05

06

07

08

09

10

11

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

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

              android:orientation="vertical"

              android:layout_width="match_parent"

              android:layout_height="match_parent">

 

    <ListView

        android:id="@+id/listview"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

</LinearLayout>

    在res/layout/目录下新建一个simpleadapter_item.xml的列表项布局文件,其代码如下:

[代码]xml代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<?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="match_parent"

              android:orientation="horizontal">

 

    <ImageView

        android:id="@+id/icon_img"

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:padding="5dp" />

 

    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical">

 

        <TextView

            android:id="@+id/title_tv"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:textSize="16sp" />

 

        <TextView

            android:id="@+id/info_tv"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:textSize="10sp" />

    </LinearLayout>

</LinearLayout>

    接下来为ListView提供Adapter,使用SimpleAdapter决定ListView所要显示的列表项。创建SimpleAdapterActivity.java文件,加载上面新建的布局文件,具体代码如下:

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

package com.jinyu.cqkxzsxy.android.listviewsample;

 

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.widget.ListView;

import android.widget.SimpleAdapter;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

public class SimpleAdapterActivity   extends AppCompatActivity   {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.simpleadapter_layout);

 

        //   获取界面组件

        ListView   listView = (ListView) findViewById(R.id.listview);

 

        //   创建一个SimpleAdapter

        SimpleAdapter   adapter = new SimpleAdapter(this,

                getData(),

                R.layout.simpleadapter_item,

                new String[]{"img", "title",   "info"},

                new int[]{R.id.icon_img, R.id.title_tv,   R.id.info_tv});

 

        //   为ListView设置Adapter

        listView.setAdapter(adapter);

    }

 

    /**

     *  创建一个List集合,其元素为Map

     * @return 返回列表项的集合对象

     */

    private List<map<string, object="">>   getData() {

        List<map<string,   object="">> list = new ArrayList<map<string, object="">>();

 

        Map<string,   object=""> map = new HashMap<string, object="">();

        map.put("img",   R.drawable.item_01);

        map.put("title",   "小宗");

        map.put("info",   "电台DJ");

        list.add(map);

 

        map   = new HashMap<string,   object="">();

        map.put("img",   R.drawable.item_02);

        map.put("title",   "貂蝉");

        map.put("info",   "四大美女");

        list.add(map);

 

        map   = new HashMap<string,   object="">();

        map.put("img",   R.drawable.item_03);

        map.put("title",   "奶茶");

        map.put("info",   "清纯妹妹");

        list.add(map);

 

        map   = new HashMap<string,   object="">();

        map.put("img",   R.drawable.item_04);

        map.put("title",   "大黄");

        map.put("info",   "是小狗");

        list.add(map);

 

        map   = new HashMap<string,   object="">();

        map.put("img",   R.drawable.item_05);

        map.put("title",   "hello");

        map.put("info",   "every thing");

        list.add(map);

 

        map   = new HashMap<string,   object="">();

        map.put("img",   R.drawable.item_06);

        map.put("title",   "world");

        map.put("info",   "hello world");

        list.add(map);

 

        return list;

    }

}

</string,></string,></string,></string,></string,></string,></string,></map<string,></map<string,></map<string,>

    上面的程序创建了一个SimpleAdapter。getData()方法生成一个长度为6的集合,意味着生成的ListView将会包含6个列表项,每个列表项都是R.layout.list_item对应的组件。

    第一个列表项的数据是{“img”=R.id.item_01, “title”=“小宗”, “info”=“电台DJ”}Map集合。创建SimpleAdapter时第5个参数、第4个参数指定使用ID为R.id.icon_img组件显示img对应的值,使用ID为R.id.title_tv组件显示title对应的值,使用ID为R.id.info_tv组件显示info对应的值,这样第一个列表项组件所包含的三个组件都有了显示的内容。后面的列表项以此类推。

    SimpleAdapter 同样可作为 ListActivity 的内容Adapter,这样可以让用户方便地定制ListActivity所显示的列表项。

    同ArrayAdapter创建ListView一样,如果需要监听用户单击、选中某个列表项的事件,则可以通过AdapterView的setOnltemClickListener()方法为单击事件添加监听器,或者通过 setOnItemSelectedListener()方法为列表项的选中事件添加监听器。

    今天就先到这里,如果有问题欢迎留言一起探讨,也欢迎加入Android零基础入门技术讨论微信群,共同成长!

原文链接:http://www.apkbus.com/blog-205190-68716.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消