ListView详解.docx

上传人:b****6 文档编号:8559444 上传时间:2023-01-31 格式:DOCX 页数:27 大小:151.40KB
下载 相关 举报
ListView详解.docx_第1页
第1页 / 共27页
ListView详解.docx_第2页
第2页 / 共27页
ListView详解.docx_第3页
第3页 / 共27页
ListView详解.docx_第4页
第4页 / 共27页
ListView详解.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

ListView详解.docx

《ListView详解.docx》由会员分享,可在线阅读,更多相关《ListView详解.docx(27页珍藏版)》请在冰豆网上搜索。

ListView详解.docx

ListView详解

  在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。

抽空把对ListView的使用做了整理,并写了个小例子,如下图。

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

1.ListVeiw用来展示列表的View。

2.适配器 用来把数据映射到ListView上的中介。

3.数据    具体的将被映射的字符串,图片,或者基本组件。

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

其中以ArrayAdapter最为简单,只能展示一行字。

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

SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。

 我们从最简单的ListView开始:

01

/**

02

 *@authorallin

03

 *

04

 */

05

publicclassMyListViewextendsActivity{

06

 

07

    privateListViewlistView;

08

    //privateListdata=newArrayList();

09

    @Override

10

    publicvoidonCreate(BundlesavedInstanceState){

11

        super.onCreate(savedInstanceState);

12

         

13

        listView=newListView(this);

14

        listView.setAdapter(newArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,getData()));

15

        setContentView(listView);

16

    }

17

     

18

     

19

     

20

    privateListgetData(){

21

         

22

        Listdata=newArrayList();

23

        data.add("测试数据1");

24

        data.add("测试数据2");

25

        data.add("测试数据3");

26

        data.add("测试数据4");

27

         

28

        returndata;

29

    }

30

}

上面代码使用了ArrayAdapter(Context context,inttextViewResourceId, Listobjects)来装配数据,要装配这些数据就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作,ArrayAdapter的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。

同时用setAdapter()完成适配的最后工作。

运行后的现实结构如下图:

SimpleCursorAdapter

  sdk的解释是这样的:

AneasyadaptertomapcolumnsfromacursortoTextViewsorImageViewsdefinedinanXMLfile.Youcanspecifywhichcolumnsyouwant,whichviewsyouwanttodisplaythecolumns,andtheXMLfilethatdefinestheappearanceoftheseviews。

简单的说就是方便把从游标得到的数据进行列表显示,并可以把指定的列映射到对应的TextView中。

  下面的程序是从电话簿中把联系人显示到类表中。

先在通讯录中添加一个联系人作为数据库的数据。

然后获得一个指向数据库的Cursor并且定义一个布局文件(当然也可以使用系统自带的)。

01

/**

02

 *@authorallin

03

 *

04

 */

05

publicclassMyListView2extendsActivity{

06

 

07

    privateListViewlistView;

08

    //privateListdata=newArrayList();

09

    @Override

10

    publicvoidonCreate(BundlesavedInstanceState){

11

        super.onCreate(savedInstanceState);

12

         

13

        listView=newListView(this);

14

         

15

        Cursorcursor=getContentResolver().query(People.CONTENT_URI,null,null,null,null);

16

        startManagingCursor(cursor);

17

         

18

        ListAdapterlistAdapter=newSimpleCursorAdapter(this,android.R.layout.simple_expandable_list_item_1,

19

                cursor,

20

                newString[]{People.NAME},

21

                newint[]{android.R.id.text1});

22

         

23

        listView.setAdapter(listAdapter);

24

        setContentView(listView);

25

    }

26

     

27

     

28

}

 Cursorcursor=getContentResolver().query(People.CONTENT_URI,null,null,null,null);先获得一个指向系统通讯录数据库的Cursor对象获得数据来源。

 startManagingCursor(cursor);我们将获得的Cursor对象交由Activity管理,这样Cursor的生命周期和Activity便能够自动同步,省去自己手动管理Cursor。

 SimpleCursorAdapter构造函数前面3个参数和ArrayAdapter是一样的,最后两个参数:

一个包含数据库的列的String型数组,一个包含布局文件中对应组件id的int型数组。

其作用是自动的将String型数组所表示的每一列数据映射到布局文件对应id的组件上。

上面的代码,将NAME列的数据一次映射到布局文件的id为text1的组件上。

注意:

需要在AndroidManifest.xml中如权限:

name="android.permission.READ_CONTACTS">

运行后效果如下图:

SimpleAdapter

simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。

下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。

下面的程序是实现一个带有图片的类表。

首先需要定义好一个用来显示每一个列内容的xml

vlist.xml

01

xmlversion="1.0"encoding="utf-8"?

>

02

android="

03

    android:

orientation="horizontal"android:

layout_width="fill_parent"

04

    android:

layout_height="fill_parent">

05

 

06

 

07

    

id="@+id/img"

08

        android:

layout_width="wrap_content"

09

        android:

layout_height="wrap_content"

10

        android:

layout_margin="5px"/>

11

 

12

    

orientation="vertical"

13

        android:

layout_width="wrap_content"

14

        android:

layout_height="wrap_content">

15

 

16

        

id="@+id/title"

17

            android:

layout_width="wrap_content"

18

            android:

layout_height="wrap_content"

19

            android:

textColor="#FFFFFFFF"

20

            android:

textSize="22px"/>

21

        

id="@+id/info"

22

            android:

layout_width="wrap_content"

23

            android:

layout_height="wrap_content"

24

            android:

textColor="#FFFFFFFF"

25

            android:

textSize="13px"/>

26

 

27

    

28

 

29

 

30

下面是实现代码:

01

/**

02

 *@authorallin

03

 *

04

 */

05

publicclassMyListView3extendsListActivity{

06

 

07

 

08

    //privateListdata=newArrayList();

09

    @Override

10

    publicvoidonCreate(BundlesavedInstanceState){

11

        super.onCreate(savedInstanceState);

12

 

13

        SimpleAdapteradapter=newSimpleAdapter(this,getData(),R.layout.vlist,

14

                newString[]{"title","info","img"},

15

                newint[]{R.id.title,R.id.info,R.id.img});

16

        setListAdapter(adapter);

17

    }

18

 

19

    privateList>getData(){

20

        List>list=newArrayList>();

21

 

22

        Mapmap=newHashMap();

23

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

24

        map.put("info","google1");

25

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

26

        list.add(map);

27

 

28

        map=newHashMap();

29

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

30

        map.put("info","google2");

31

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

32

        list.add(map);

33

 

34

        map=newHashMap();

35

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

36

        map.put("info","google3");

37

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

38

        list.add(map);

39

         

40

        returnlist;

41

    }

42

}

使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。

HashMap的每个键值数据映射到布局文件中对应id的组件上。

因为系统没有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。

下面做适配,new一个SimpleAdapter参数一次是:

this,布局文件(vlist.xml),HashMap的title和info,img。

布局文件的组件id,title,info,img。

布局文件的各组件分别映射到HashMap的各元素上,完成适配。

运行效果如下图:

有按钮的ListView

但是有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。

添加按钮首先要写一个有按钮的xml文件,然后自然会想到用上面的方法定义一个适配器,然后将数据映射到布局文件上。

但是事实并非这样,因为按钮是无法映射的,即使你成功的用布局文件显示出了按钮也无法添加按钮的响应,这时就要研究一下ListView是如何现实的了,而且必须要重写一个类继承BaseAdapter。

下面的示例将显示一个按钮和一个图片,两行字如果单击按钮将删除此按钮的所在行。

并告诉你ListView究竟是如何工作的。

效果如下:

vlist2.xml

01

xmlversion="1.0"encoding="utf-8"?

>

02

android="

03

    android:

orientation="horizontal"

04

    android:

layout_width="fill_parent"

05

    android:

layout_height="fill_parent">

06

 

07

 

08

    

id="@+id/img"

09

        android:

layout_width="wrap_content"

10

        android:

layout_height="wrap_content"

11

        android:

layout_margin="5px"/>

12

 

13

    

orientation="vertical"

14

        android:

layout_width="wrap_content"

15

        android:

layout_height="wrap_content">

16

 

17

        

id="@+id/title"

18

            android:

layout_width="wrap_content"

19

            android:

layout_height="wrap_content"

20

            android:

textColor="#FFFFFFFF"

21

            android:

textSize="22px"/>

22

        

id="@+id/info"

23

            android:

layout_width="wrap_content"

24

            android:

layout_height="wrap_content"

25

            android:

textColor="#FFFFFFFF"

26

            android:

textSize="13px"/>

27

 

28

    

29

 

30

 

31

    

id="@+id/view_btn"

32

        android:

layout_width="wrap_content"

33

        android:

layout_height="wrap_content"

34

        android:

text="@string/s_view_btn"

35

        android:

layout_gravity="bottom|right"/>

36

程序代码:

001

/**

002

 *@authorallin

003

 *

004

 */

005

publicclassMyListView4extendsListActivity{

006

 

007

 

008

    privateList>mData;

009

     

010

    @Override

011

    publicvoidonCreate(BundlesavedInstanceState){

012

        super.onCreate(savedInstanceState);

013

        mData=getData();

014

        MyAdapteradapter=newMyAdapter(this);

015

        setListAdapter(adapter);

016

    }

017

 

018

    privateList>getData(){

019

        List>list=newArrayList>();

020

 

021

        Mapmap=newHashMap();

022

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

023

        map.put("info","g

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1