本文主要实现在自定义的listview布局中加入checkbox控件,通过判断用户是否选中checkbox来对listview的选中项进行相应的操作。通过一个demo来展示该功能,选中listview中的某一项,然后点击button按钮来显示选中了哪些项。
1、程序结构图如下:
其中person.java是实体类,mainactivity.java是activity组件类。listitem.xml是自定义的列表每项布局文件。
2、listitem.xml布局文件源码如下:
1
2
3
4
5
6
7
8
9
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <linearlayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:orientation= "horizontal" android:descendantfocusability= "blocksdescendants" > <checkbox android:id= "@+id/list.select" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> <textview android:id= "@+id/list.name" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_weight= "1" android:text= "name" android:layout_gravity= "center" android:textsize= "20dp" android:layout_marginleft= "10dp" /> <textview android:id= "@+id/list.address" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_weight= "1" android:text= "address" android:layout_gravity= "center" android:textsize= "20dp" /> </linearlayout> </linearlayout> |
3、 main.xml布局文件源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version= "1.0" encoding= "utf-8" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <button android:id= "@+id/show" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "show" /> <listview android:id= "@+id/lvperson" android:layout_width= "fill_parent" android:layout_height= "fill_parent" /> </linearlayout> |
4、person.java实体类源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.andyidea.bean; public class person { private string name; private string address; public string getname() { return name; } public void setname(string name) { this .name = name; } public string getaddress() { return address; } public void setaddress(string address) { this .address = address; } } |
5、mainactivity.java类源码如下:
1
2
3
4
5
6
7
8
9
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
package com.andyidea.listview; import java.util.arraylist; import java.util.hashmap; import java.util.list; import com.andyidea.bean.person; import android.app.activity; import android.app.alertdialog; import android.content.context; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.button; import android.widget.checkbox; import android.widget.listview; import android.widget.textview; public class mainactivity extends activity { button show; listview lv; list<person> persons = new arraylist<person>(); context mcontext; mylistadapter adapter; list<integer> listitemid = new arraylist<integer>(); /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); mcontext = getapplicationcontext(); show = (button)findviewbyid(r.id.show); lv = (listview)findviewbyid(r.id.lvperson); initpersondata(); adapter = new mylistadapter(persons); lv.setadapter(adapter); show.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { listitemid.clear(); for ( int i= 0 ;i<adapter.mchecked.size();i++){ if (adapter.mchecked.get(i)){ listitemid.add(i); } } if (listitemid.size()== 0 ){ alertdialog.builder builder1 = new alertdialog.builder(mainactivity. this ); builder1.setmessage( "没有选中任何记录" ); builder1.show(); } else { stringbuilder sb = new stringbuilder(); for ( int i= 0 ;i<listitemid.size();i++){ sb.append( "itemid=" +listitemid.get(i)+ " . " ); } alertdialog.builder builder2 = new alertdialog.builder(mainactivity. this ); builder2.setmessage(sb.tostring()); builder2.show(); } } }); } /** * 模拟数据 */ private void initpersondata(){ person mperson; for ( int i= 1 ;i<= 12 ;i++){ mperson = new person(); mperson.setname( "andy" +i); mperson.setaddress( "guangzhou" +i); persons.add(mperson); } } //自定义listview适配器 class mylistadapter extends baseadapter{ list< boolean > mchecked; list<person> listperson; hashmap<integer,view> map = new hashmap<integer,view>(); public mylistadapter(list<person> list){ listperson = new arraylist<person>(); listperson = list; mchecked = new arraylist< boolean >(); for ( int i= 0 ;i<list.size();i++){ mchecked.add( false ); } } @override public int getcount() { return listperson.size(); } @override public object getitem( int position) { return listperson.get(position); } @override public long getitemid( int position) { return position; } @override public view getview( int position, view convertview, viewgroup parent) { view view; viewholder holder = null ; if (map.get(position) == null ) { log.e( "mainactivity" , "position1 = " +position); layoutinflater minflater = (layoutinflater) mcontext .getsystemservice(context.layout_inflater_service); view = minflater.inflate(r.layout.listitem, null ); holder = new viewholder(); holder.selected = (checkbox)view.findviewbyid(r.id.list_select); holder.name = (textview)view.findviewbyid(r.id.list_name); holder.address = (textview)view.findviewbyid(r.id.list_address); final int p = position; map.put(position, view); holder.selected.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { checkbox cb = (checkbox)v; mchecked.set(p, cb.ischecked()); } }); view.settag(holder); } else { log.e( "mainactivity" , "position2 = " +position); view = map.get(position); holder = (viewholder)view.gettag(); } holder.selected.setchecked(mchecked.get(position)); holder.name.settext(listperson.get(position).getname()); holder.address.settext(listperson.get(position).getaddress()); return view; } } static class viewholder{ checkbox selected; textview name; textview address; } } |
程序运行后的结果如下:
希望本文所述对大家学习android软件编程有所帮助。