androidListView详解
在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。抽空把对ListView的使用做了整理,并写了个小例子,如下图。
列表的显示需要三个元素:
1.ListVeiw用来展示列表的View。
2.适配器用来把数据映射到ListView上的中介。
3.数据具体的将被映射的字符串,图片,或者基本组件。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。
SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。
我们从最简单的ListView开始:
viewsource
print
?
05
| public
class
MyListView
extends
Activity{
|
07
|
private
ListViewlistView;
|
08
|
//privateList<String>data=newArrayList<String>();
|
10
|
public
void
onCreate(BundlesavedInstanceState){
|
11
|
super
.onCreate(savedInstanceState);
|
13
|
listView=
new
ListView(
this
);
|
14
|
listView.setAdapter(
new
ArrayAdapter<String>(
this
,android.R.layout.simple_expandable_list_item_1,getData()));
|
15
|
setContentView(listView);
|
20
|
private
List<String>getData(){
|
22
|
List<String>data=
new
ArrayList<String>();
|
上面代码使用了ArrayAdapter
(Context
context,inttextViewResourceId,List
<T>objects)来装配数据,要装配这些数据就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作,ArrayAdapter
的
构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布
局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时
用setAdapter()完成适配的最后工作。运行后的现实结构如下图:
SimpleCursorAdapter
sdk的解释是这样的:Aneasyadaptertomapcolumnsfromacursorto
TextViewsorImageViewsdefinedinanXMLfile.Youcanspecifywhich
columnsyouwant,whichviewsyouwanttodisplaythecolumns,andthe
XMLfilethatdefinestheappearanceofthese
views。简单的说就是方便把从游标得到的数据进行列表显示,并可以把指定的列映射到对应的TextView中。
下面的程序是从电话簿中把联系人显示到类表中。先在通讯录中添加一个联系人作为数据库的数据。然后获得一个指向数据库的Cursor并且定义一个布局文件(当然也可以使用系统自带的)。
viewsource
print
?
05
| public
class
MyListView2
extends
Activity{
|
07
|
private
ListViewlistView;
|
08
|
//privateList<String>data=newArrayList<String>();
|
10
|
public
void
onCreate(BundlesavedInstanceState){
|
11
|
super
.onCreate(savedInstanceState);
|
13
|
listView=
new
ListView(
this
);
|
15
|
Cursorcursor=getContentResolver().query(People.CONTENT_URI,
null
,
null
,
null
,
null
);
|
16
|
startManagingCursor(cursor);
|
18
|
ListAdapterlistAdapter=
new
SimpleCursorAdapter(
this
,android.R.layout.simple_expandable_list_item_1,
|
20
|
new
String[]{People.NAME},
|
21
|
new
int
[]{android.R.id.text1});
|
23
|
listView.setAdapter(listAdapter);
|
24
|
setContentView(listView);
|
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中如权限:<uses-permissionandroid:name="android.permission.READ_CONTACTS"></uses-permission>
运行后效果如下图:
SimpleAdapter
simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按
钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的
差别,不同就是对显示ListView做了许多优化,方面显示而已。
下面的程序是实现一个带有图片的类表。
首先需要定义好一个用来显示每一个列内容的xml
vlist.xml
viewsource
print
?
01
| <?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
|
02
| <
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android "
|
03
|
android:orientation
=
"horizontal"
android:layout_width
=
"fill_parent"
|
04
|
android:layout_height
=
"fill_parent"
>
|
07
|
<
ImageView
android:id
=
"@+id/img"
|
08
|
android:layout_width
=
"wrap_content"
|
09
|
android:layout_height
=
"wrap_content"
|
10
|
android:layout_margin
=
"5px"
/>
|
12
|
<
LinearLayout
android:orientation
=
"vertical"
|
13
|
android:layout_width
=
"wrap_content"
|
14
|
android:layout_height
=
"wrap_content"
>
|
16
|
<
TextView
android:id
=
"@+id/title"
|
17
|
android:layout_width
=
"wrap_content"
|
18
|
android:layout_height
=
"wrap_content"
|
19
|
android:textColor
=
"#FFFFFFFF"
|
20
|
android:textSize
=
"22px"
/>
|
21
|
<
TextView
android:id
=
"@+id/info"
|
22
|
android:layout_width
=
"wrap_content"
|
23
|
android:layout_height
=
"wrap_content"
|
24
|
android:textColor
=
"#FFFFFFFF"
|
25
|
android:textSize
=
"13px"
/>
|
下面是实现代码:
viewsource
print
?
05
| public
class
MyListView3
extends
ListActivity{
|
08
|
//privateList<String>data=newArrayList<String>();
|
10
|
public
void
onCreate(BundlesavedInstanceState){
|
11
|
super
.onCreate(savedInstanceState);
|
13
|
SimpleAdapteradapter=
new
SimpleAdapter(
this
,getData(),R.layout.vlist,
|
14
|
new
String[]{
"title"
,
"info"
,
"img"
},
|
15
|
new
int
[]{R.id.title,R.id.info,R.id.img});
|
16
|
setListAdapter(adapter);
|
19
|
private
List<Map<String,Object>>getData(){
|
20
|
List<Map<String,Object>>list=
new
ArrayList<Map<String,Object>>();
|
22
|
Map<String,Object>map=
new
HashMap<String,Object>();
|
24
|
map.put(
"info"
,
"google1"
);
|
25
|
map.put(
"img"
,R.drawable.i1);
|
28
|
map=
new
HashMap<String,Object>();
|
30
|
map.put(
"info"
,
"google2"
);
|
31
|
map.put(
"img"
,R.drawable.i2);
|
34
|
map=
new
HashMap<String,Object>();
|
36
|
map.put(
"info"
,
"google3"
);
|
37
|
map.put(
"img"
,R.drawable.i3);
|
使用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
viewsource
print
?
01
| <?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
|
02
| <
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android "
|
03
|
android:orientation
=
"horizontal"
|
04
|
android:layout_width
=
"fill_parent"
|
05
|
android:layout_height
=
"fill_parent"
>
|
08
|
<
ImageView
android:id
=
"@+id/img"
|
09
|
android:layout_width
=
"wrap_content"
|
10
|
android:layout_height
=
"wrap_content"
|
11
|
android:layout_margin
=
"5px"
/>
|
13
|
<
LinearLayout
android:orientation
=
"vertical"
|
14
|
android:layout_width
=
"wrap_content"
|
15
|
android:layout_height
=
"wrap_content"
>
|
17
|
<
TextView
android:id
=
"@+id/title"
|
18
|
android:layout_width
=
"wrap_content"
|
19
|
android:layout_height
=
"wrap_content"
|
20
|
android:textColor
=
"#FFFFFFFF"
|
21
|
android:textSize
=
"22px"
/>
|
22
|
<
TextView
android:id
=
"@+id/info"
|
23
|
android:layout_width
=
"wrap_content"
|
24
|
android:layout_height
=
"wrap_content"
|
25
|
android:textColor
=
"#FFFFFFFF"
|
26
|
android:textSize
=
"13px"
/>
|
31
|
<
Button
android: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"
/>
|
程序代码:
viewsource
print
?
005
| public
class
MyListView4
extends
ListActivity{
|
008
|
private
List<Map<String,Object>>mData;
|
011
|
public
void
onCreate(BundlesavedInstanceState){
|
012
|
super
.onCreate(savedInstanceState);
|
014
|
MyAdapteradapter=
new
MyAdapter(
this
);
|
015
|
setListAdapter(adapter);
|
018
|
private
List<Map<String,Object>>getData(){
|
019
|
List<Map<String,Object>>list=
new
ArrayList<Map<String,Object>>();
|
021
|
Map<String,Object>map=
new
HashMap<String,Object>();
|
022
|
map.put(
"title"
,
"G1"
);
|
023
|
map.put(
"info"
,
"google1"
);
|
024
|
map.put(
"img"
,R.drawable.i1);
|
027
|
map=
new
HashMap<String,Object>();
|
028
|
map.put(
"title"
,
"G2"
);
|
029
|
map.put(
"info"
,
"google2"
);
|
030
|
map.put(
"img"
,R.drawable.i2);
|
033
|
map=
new
HashMap<String,Object>();
|
034
|
map.put(
"title"
,
"G3"
);
|
035
|
map.put(
"info"
,
"google3"
);
|
036
|
map.put(
"img"
,R.drawable.i3);
|
044
|
protected
void
onListItemClick(ListViewl,Viewv,
int
position,
long
id){
|
046
|
Log.v(
"MyListView4-click"
,(String)mData.get(position).get(
"title"
));
|
053
|
new
AlertDialog.Builder(
this
)
|
054
|
.setTitle(
"我的listview"
)
|
056
|
.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener(){
|
058
|
public
void
onClick(DialogInterfacedialog,
int
which){
|
067
|
public
final
class
ViewHolder{
|
075
|
public
class
MyAdapter
extends
BaseAdapter{
|
077
|
private
LayoutInflatermInflater;
|
080
|
public
MyAdapter(Contextcontext){
|
081
|
this
.mInflater=LayoutInflater.from(context);
|
085
|
//TODOAuto-generatedmethodstub
|
090
|
public
ObjectgetItem(
int
arg0){
|
091
|
//TODOAuto-generatedmethodstub
|
096
|
public
long
getItemId(
int
arg0){
|
097
|
//TODOAuto-generatedmethodstub
|
102
|
public
ViewgetView(
int
position,ViewconvertView,ViewGroupparent){
|
104
|
ViewHolderholder=
null
;
|
105
|
if
(convertView==
null
){
|
107
|
holder=
new
ViewHolder();
|
109
|
convertView=mInflater.inflate(R.layout.vlist2,
null
);
|
110
|
holder.img=(ImageView)convertView.findViewById(R.id.img);
|
111
|
holder.title=(TextView)convertView.findViewById(R.id.title);
|
112
|
holder.info=(TextView)convertView.findViewById(R.id.info);
|
113
|
holder.viewBtn=(Button)convertView.findViewById(R.id.view_btn);
|
114
|
convertView.setTag(holder);
|
118
|
holder=(ViewHolder)convertView.getTag();
|
122
|
holder.img.setBackgroundResource((Integer)mData.get(position).get(
"img"
));
|
123
|
holder.title.setText((String)mData.get(position).get(
"title"
));
|
124
|
holder.info.setText((String)mData.get(position).get(
"info"
));
|
126
|
holder.viewBtn.setOnClickListener(
new
View.OnClickListener(){
|
129
|
public
void
onClick(Viewv){
|
下面将对上述代码,做详细的解释,listView在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得到
listView的长度(这也是为什么在开始的第一张图特别的标出列表长度),然后根据这个长度,调用getView()逐一绘制每一行。如果你的
getCount()返回值是0的话,列表将不显示同样return1,就只显示一行。
系统显示列表时,首先实例化一个适配器(这里将实例化自定义的适配器)。当手动完成适配时,必须手动映射数据,这需要重写getView()方
法。系统在绘制列表的每一行的时候将调用此方法。getView()有三个参数,position表示将显示的是第几行,covertView是从布局文
件中inflate来的布局。我们用LayoutInflater的方法将定义好的vlist2.xml文件提取成View实例用来显示。然后将xml文
件中的各个组件实例化(简单的findViewById()方法)。这样便可以将数据对应到各个组件上了。但是按钮为了响应点击事件,需要为它添加点击监
听器,这样就能捕获点击事件。至此一个自定义的listView就完成了,现在让我们回过头从新审视这个过程。系统要绘制ListView了,他首先获得
要绘制的这个列表的长度,然后开始绘制第一行,怎么绘制呢?调用getView()函数。在这个函数里面首先获得一个View(实际上是一个
ViewGroup),然后再实例并设置各个组件,显示之。好了,绘制完这一行了。那
再绘制下一行,直到绘完为止。在实际的运行过程中会发现listView的每一行没有焦点了,这是因为Button抢夺了listView的焦点,只要布局文件中将Button设置为没有焦点就OK了。
运行效果如下图:
源码下载
在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。抽空把对ListView的使用做了整理,并写了个小例子,如下图。
列表的显示需要三个元素:
1.ListVeiw用来展示列表的View。
2.适配器用来把数据映射到ListView上的中介。
3.数据具体的将被映射的字符串,图片,或者基本组件。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。
SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。
我们从最简单的ListView开始:
viewsource
print
?
05
| public
class
MyListView
extends
Activity{
|
07
|
private
ListViewlistView;
|
08
|
//privateList<String>data=newArrayList<String>();
|
10
|
public
void
onCreate(BundlesavedInstanceState){
|
11
|
super
.onCreate(savedInstanceState);
|
13
|
listView=
new
ListView(
this
);
|
14
|
listView.setAdapter(
new
ArrayAdapter<String>(
this
,android.R.layout.simple_expandable_list_item_1,getData()));
|
15
|
setContentView(listView);
|
20
|
private
List<String>getData(){
|
22
|
List<String>data=
new
ArrayList<String>();
|
上面代码使用了ArrayAdapter
(Context
context,inttextViewResourceId,List
<T>objects)来装配数据,要装配这些数据就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作,ArrayAdapter
的
构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布
局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时
用setAdapter()完成适配的最后工作。运行后的现实结构如下图:
SimpleCursorAdapter
sdk的解释是这样的:Aneasyadaptertomapcolumnsfromacursorto
TextViewsorImageViewsdefinedinanXMLfile.Youcanspecifywhich
columnsyouwant,whichviewsyouwanttodisplaythecolumns,andthe
XMLfilethatdefinestheappearanceofthese
views。简单的说就是方便把从游标得到的数据进行列表显示,并可以把指定的列映射到对应的TextView中。
下面的程序是从电话簿中把联系人显示到类表中。先在通讯录中添加一个联系人作为数据库的数据。然后获得一个指向数据库的Cursor并且定义一个布局文件(当然也可以使用系统自带的)。
viewsource
print
?
05
| public
class
MyListView2
extends
Activity{
|
07
|
private
ListViewlistView;
|
08
|
//privateList<String>data=newArrayList<String>();
|
10
|
public
void
onCreate(BundlesavedInstanceState){
|
11
|
super
.onCreate(savedInstanceState);
|
13
|
listView=
new
ListView(
this
);
|
15
|
Cursorcursor=getContentResolver().query(People.CONTENT_URI,
null
,
null
,
null
,
null
);
|
16
|
startManagingCursor(cursor);
|
18
|
ListAdapterlistAdapter=
new
SimpleCursorAdapter(
this
,android.R.layout.simple_expandable_list_item_1,
|
20
|
new
String[]{People.NAME},
|
21
|
new
int
[]{android.R.id.text1});
|
23
|
listView.setAdapter(listAdapter);
|
24
|
setContentView(listView);
|
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中如权限:<uses-permissionandroid:name="android.permission.READ_CONTACTS"></uses-permission>
运行后效果如下图:
SimpleAdapter
simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按
钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的
差别,不同就是对显示ListView做了许多优化,方面显示而已。
下面的程序是实现一个带有图片的类表。
首先需要定义好一个用来显示每一个列内容的xml
vlist.xml
viewsource
print
?
01
| <?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
|
02
| <
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android "
|
03
|
android:orientation
=
"horizontal"
android:layout_width
=
"fill_parent"
|
04
|
android:layout_height
=
"fill_parent"
>
|
07
|
<
ImageView
android:id
=
"@+id/img"
|
08
|
android:layout_width
=
"wrap_content"
|
09
|
android:layout_height
=
"wrap_content"
|
10
|
android:layout_margin
=
"5px"
/>
|
12
|
<
LinearLayout
android:orientation
=
"vertical"
|
13
|
android:layout_width
=
"wrap_content"
|
14
|
android:layout_height
=
"wrap_content"
>
|
16
|
<
TextView
android:id
=
"@+id/title"
|
17
|
android:layout_width
=
"wrap_content"
|
18
|
android:layout_height
=
"wrap_content"
|
19
|
android:textColor
=
"#FFFFFFFF"
|
20
|
android:textSize
=
"22px"
/>
|
21
|
<
TextView
android:id
=
"@+id/info"
|
22
|
android:layout_width
=
"wrap_content"
|
23
|
android:layout_height
=
"wrap_content"
|
24
|
android:textColor
=
"#FFFFFFFF"
|
25
|
android:textSize
=
"13px"
/>
|
下面是实现代码:
viewsource
print
?
05
| public
class
MyListView3
extends
ListActivity{
|
08
|
//privateList<String>data=newArrayList<String>();
|
10
|
public
void
onCreate(BundlesavedInstanceState){
|
11
|
super
.onCreate(savedInstanceState);
|
13
|
SimpleAdapteradapter=
new
SimpleAdapter(
this
,getData(),R.layout.vlist,
|
14
|
new
String[]{
"title"
,
"info"
,
"img"
},
|
15
|
new
int
[]{R.id.title,R.id.info,R.id.img});
|
16
|
setListAdapter(adapter);
|
19
|
private
List<Map<String,Object>>getData(){
|
20
|
List<Map<String,Object>>list=
new
ArrayList<Map<String,Object>>();
|
22
|
Map<String,Object>map=
new
HashMap<String,Object>();
|
24
|
map.put(
"info"
,
"google1"
);
|
25
|
map.put(
"img"
,R.drawable.i1);
|
28
|
map=
new
HashMap<String,Object>();
|
30
|
map.put(
"info"
,
"google2"
);
|
31
|
map.put(
"img"
,R.drawable.i2);
|
34
|
map=
new
HashMap<String,Object>();
|
36
|
map.put(
"info"
,
"google3"
);
|
37
|
map.put(
"img"
,R.drawable.i3);
|
使用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
viewsource
print
?
01
| <?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
|
02
| <
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android "
|
03
|
android:orientation
=
"horizontal"
|
04
|
android:layout_width
=
"fill_parent"
|
05
|
android:layout_height
=
"fill_parent"
>
|
08
|
<
ImageView
android:id
=
"@+id/img"
|
09
|
android:layout_width
=
"wrap_content"
|
10
|
android:layout_height
=
"wrap_content"
|
11
|
android:layout_margin
=
"5px"
/>
|
13
|
<
LinearLayout
android:orientation
=
"vertical"
|
14
|
android:layout_width
=
"wrap_content"
|
15
|
android:layout_height
=
"wrap_content"
>
|
17
|
<
TextView
android:id
=
"@+id/title"
|
18
|
android:layout_width
=
"wrap_content"
|
19
|
android:layout_height
=
"wrap_content"
|
20
|
android:textColor
=
"#FFFFFFFF"
|
21
|
android:textSize
=
"22px"
/>
|
22
|
<
TextView
android:id
=
"@+id/info"
|
23
|
android:layout_width
=
"wrap_content"
|
24
|
android:layout_height
=
"wrap_content"
|
25
|
android:textColor
=
"#FFFFFFFF"
|
26
|
android:textSize
=
"13px"
/>
|
31
|
<
Button
android: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"
/>
|
程序代码:
viewsource
print
?
005
| public
class
MyListView4
extends
ListActivity{
|
008
|
private
List<Map<String,Object>>mData;
|
011
|
public
void
onCreate(BundlesavedInstanceState){
|
012
|
super
.onCreate(savedInstanceState);
|
014
|
MyAdapteradapter=
new
MyAdapter(
this
);
|
015
|
setListAdapter(adapter);
|
018
|
private
List<Map<String,Object>>getData(){
|
019
|
List<Map<String,Object>>list=
new
ArrayList<Map<String,Object>>();
|
021
|
Map<String,Object>map=
new
HashMap<String,Object>();
|
022
|
map.put(
"title"
,
"G1"
);
|
023
|
map.put(
"info"
,
"google1"
);
|
024
|
map.put(
"img"
,R.drawable.i1);
|
027
|
map=
new
HashMap<String,Object>();
|
028
|
map.put(
"title"
,
"G2"
);
|
029
|
map.put(
"info"
,
"google2"
);
|
030
|
map.put(
"img"
,R.drawable.i2);
|
033
|
map=
new
HashMap<String,Object>();
|
034
|
map.put(
"title"
,
"G3"
);
|
035
|
map.put(
"info"
,
"google3"
);
|
036
|
map.put(
"img"
,R.drawable.i3);
|
044
|
protected
void
onListItemClick(ListViewl,Viewv,
int
position,
long
id){
|
046
|
Log.v(
"MyListView4-click"
,(String)mData.get(position).get(
"title"
));
|
053
|
new
AlertDialog.Builder(
this
)
|
054
|
.setTitle(
"我的listview"
)
|
056
|
.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener(){
|
058
|
public
void
onClick(DialogInterfacedialog,
int
which){
|
067
|
public
final
class
ViewHolder{
|
075
|
public
class
MyAdapter
extends
BaseAdapter{
|
077
|
private
LayoutInflatermInflater;
|
080
|
public
MyAdapter(Contextcontext){
|
081
|
this
.mInflater=LayoutInflater.from(context);
|
085
|
//TODOAuto-generatedmethodstub
|
090
|
public
ObjectgetItem(
int
arg0){
|
091
|
//TODOAuto-generatedmethodstub
|
096
|
public
long
getItemId(
int
arg0){
|
097
|
//TODOAuto-generatedmethodstub
|
102
|
public
ViewgetView(
int
position,ViewconvertView,ViewGroupparent){
|
104
|
ViewHolderholder=
null
;
|
105
|
if
(convertView==
null
){
|
107
|
holder=
new
ViewHolder();
|
109
|
convertView=mInflater.inflate(R.layout.vlist2,
null
);
|
110
|
holder.img=(ImageView)convertView.findViewById(R.id.img);
|
111
|
holder.title=(TextView)convertView.findViewById(R.id.title);
|
112
|
holder.info=(TextView)convertView.findViewById(R.id.info);
|
113
|
holder.viewBtn=(Button)convertView.findViewById(R.id.view_btn);
|
114
|
convertView.setTag(holder);
|
118
|
holder=(ViewHolder)convertView.getTag();
|
122
|
holder.img.setBackgroundResource((Integer)mData.get(position).get(
"img"
));
|
123
|
holder.title.setText((String)mData.get(position).get(
"title"
));
|
124
|
holder.info.setText((String)mData.get(position).get(
"info"
));
|
126
|
holder.viewBtn.setOnClickListener(
new
View.OnClickListener(){
|
129
|
public
void
onClick(Viewv){
|
下面将对上述代码,做详细的解释,listView在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得到
listView的长度(这也是为什么在开始的第一张图特别的标出列表长度),然后根据这个长度,调用getView()逐一绘制每一行。如果你的
getCount()返回值是0的话,列表将不显示同样return1,就只显示一行。
系统显示列表时,首先实例化一个适配器(这里将实例化自定义的适配器)。当手动完成适配时,必须手动映射数据,这需要重写getView()方
法。系统在绘制列表的每一行的时候将调用此方法。getView()有三个参数,position表示将显示的是第几行,covertView是从布局文
件中inflate来的布局。我们用LayoutInflater的方法将定义好的vlist2.xml文件提取成View实例用来显示。然后将xml文
件中的各个组件实例化(简单的findViewById()方法)。这样便可以将数据对应到各个组件上了。但是按钮为了响应点击事件,需要为它添加点击监
听器,这样就能捕获点击事件。至此一个自定义的listView就完成了,现在让我们回过头从新审视这个过程。系统要绘制ListView了,他首先获得
要绘制的这个列表的长度,然后开始绘制第一行,怎么绘制呢?调用getView()函数。在这个函数里面首先获得一个View(实际上是一个
ViewGroup),然后再实例并设置各个组件,显示之。好了,绘制完这一行了。那
再绘制下一行,直到绘完为止。在实际的运行过程中会发现listView的每一行没有焦点了,这是因为Button抢夺了listView的焦点,只要布局文件中将Button设置为没有焦点就OK了。
运行效果如下图:
源码下载