上一节中一些列表集合数据到手机屏幕时,通常采用ListView组件+ArrayAdapter.
虽然它能为我们提供展示数据列表的能力,但是展示的项却不能定制,如果我们的项是由2个TextView组成的,它就无能为力了。项目中大部分的不单单是展示简单的项模板,更多时候,我们可以对项模板进行一些定制,来满足我们的需求,假设项模板需要展示2个TextView 呢?怎么办?
我们可以使用SimpleAdapter+ListView来实现。
SimpleAdapter其中一个构造函数如下:
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
第一个参数:当前上下文对象。第二个参数:一个List类型的泛型集合,泛型类型必须继承之Map类型。第三个:布局资源的ID,
第四个参数:需要绑定的Key列表。第五个参数:与Key列表项对应的资源文件中的具体组件ID集合。
有以上的理论基础,我们知道使用SimpleAdapter会带来这样的好处:
1:可以自定义任何项模板,自由度很高(取决于美工水平)
2:可以为项模板指定一个匹配的数据
我们先来看看项模板的布局,很简单,就是一个ImageView,两个TextView

1<? 2 xml version="1.0" encoding="utf-8" ?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent" android:layout_height 3 4 5 6 7 ="match_parent" 8 > 9 < 10 ImageView android:layout_width ="wrap_content" 11 12 13 14 15 android:layout_height 16 17 18 19 20 ="wrap_content" 21 22 23 24 25 android:id 26 27 28 29 30 ="@+id/img" 31 32 33 34 35 > 36 37 38 39 40 41 42 43 44 45 </ 46 47 48 49 50 ImageView 51 52 53 54 55 > 56 57 58 59 60 61 62 63 64 65 < 66 67 68 69 70 TextView 71 72 73 74 75 android:id 76 77 78 79 80 ="@+id/txtName" 81 82 83 84 85 android:layout_width 86 87 88 89 90 ="wrap_content" 91 92 93 94 95 android:layout_height 96 97 98 99 100 ="wrap_content" 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 TextView 126 127 128 129 130 > 131 132 133 134 135 136 137 138 139 140 < 141 142 143 144 145 TextView 146 147 148 149 150 android:paddingLeft 151 152 153 154 155 ="20sp" 156 157 158 159 160 android:id 161 162 163 164 165 ="@+id/txtLength" 166 167 168 169 170 android:layout_width 171 172 173 174 175 ="wrap_content" 176 177 178 179 180 android:layout_height 181 182 183 184 185 ="wrap_content" 186 187 188 189 190 191 192 193 194 195 > 196 197 198 199 200 201 202 203 204 205 </ 206 207 208 209 210 TextView 211 212 213 214 215 > 216 217 218 219 220 221 222 223 224 225 </ 226 227 228 229 230 LinearLayout 231 232 233 234 235 > 236 237 238 239 240 241 242 243
我们可以定义数据源,当然这个数据源是继承自List集合接口的,并且类型为基础Map接口。如下:

1List 2 3 4 5 6 < 7 8 9 10 11 Map 12 13 14 15 16 < 17 18 19 20 21 String,Object 22 23 24 25 26 >> 27 28 29 30 31 lists 32 33 34 35 36 = 37 38 39 40 41 new 42 43 44 45 46 ArrayList 47 48 49 50 51 < 52 53 54 55 56 Map 57 58 59 60 61 < 62 63 64 65 66 String,Object 67 68 69 70 71 >> 72 73 74 75 76 (); 77 78 79 80 81 for 82 83 84 85 86 ( 87 88 89 90 91 int 92 93 94 95 96 i 97 98 99 100 101 = 102 103 104 105 106 0 107 108 109 110 111 ;i 112 113 114 115 116 < 117 118 119 120 121 4 122 123 124 125 126 ;i 127 128 129 130 131 ++ 132 133 134 135 136 ){ Map 137 138 139 140 141 < 142 143 144 145 146 String,Object 147 148 149 150 151 > 152 153 154 155 156 map 157 158 159 160 161 = 162 163 164 165 166 new 167 168 169 170 171 HashMap 172 173 174 175 176 < 177 178 179 180 181 String,Object 182 183 184 185 186 > 187 188 189 190 191 (); map.put( 192 193 194 195 196 " 197 198 199 200 201 img 202 203 204 205 206 " 207 208 209 210 211 , R.drawable.icon); map.put( 212 213 214 215 216 " 217 218 219 220 221 name 222 223 224 225 226 " 227 228 229 230 231 , 232 233 234 235 236 " 237 238 239 240 241 SimpleAdapter 242 243 244 245 246 " 247 248 249 250 251 + 252 253 254 255 256 i); map.put( 257 258 259 260 261 " 262 263 264 265 266 length 267 268 269 270 271 " 272 273 274 275 276 , 277 278 279 280 281 " 282 283 284 285 286 300 287 288 289 290 291 " 292 293 294 295 296 ); lists.add(map); } 297 298 299 300 301 302 303 304
然后我们希望绑定这些数据,到指定的组件中去,全部代码如下:

1List 2 3 4 5 6 < 7 8 9 10 11 Map 12 13 14 15 16 < 17 18 19 20 21 String,Object 22 23 24 25 26 >> 27 28 29 30 31 lists 32 33 34 35 36 = 37 38 39 40 41 new 42 43 44 45 46 ArrayList 47 48 49 50 51 < 52 53 54 55 56 Map 57 58 59 60 61 < 62 63 64 65 66 String,Object 67 68 69 70 71 >> 72 73 74 75 76 (); 77 78 79 80 81 for 82 83 84 85 86 ( 87 88 89 90 91 int 92 93 94 95 96 i 97 98 99 100 101 = 102 103 104 105 106 0 107 108 109 110 111 ;i 112 113 114 115 116 < 117 118 119 120 121 4 122 123 124 125 126 ;i 127 128 129 130 131 ++ 132 133 134 135 136 ){ Map 137 138 139 140 141 < 142 143 144 145 146 String,Object 147 148 149 150 151 > 152 153 154 155 156 map 157 158 159 160 161 = 162 163 164 165 166 new 167 168 169 170 171 HashMap 172 173 174 175 176 < 177 178 179 180 181 String,Object 182 183 184 185 186 > 187 188 189 190 191 (); map.put( 192 193 194 195 196 " 197 198 199 200 201 img 202 203 204 205 206 " 207 208 209 210 211 , R.drawable.icon); map.put( 212 213 214 215 216 " 217 218 219 220 221 name 222 223 224 225 226 " 227 228 229 230 231 , 232 233 234 235 236 " 237 238 239 240 241 SimpleAdapter 242 243 244 245 246 " 247 248 249 250 251 + 252 253 254 255 256 i); map.put( 257 258 259 260 261 " 262 263 264 265 266 length 267 268 269 270 271 " 272 273 274 275 276 , 277 278 279 280 281 " 282 283 284 285 286 300 287 288 289 290 291 " 292 293 294 295 296 ); lists.add(map); } String []from 297 298 299 300 301 = 302 303 304 305 306 { 307 308 309 310 311 " 312 313 314 315 316 img 317 318 319 320 321 " 322 323 324 325 326 , 327 328 329 330 331 " 332 333 334 335 336 name 337 338 339 340 341 " 342 343 344 345 346 , 347 348 349 350 351 " 352 353 354 355 356 length 357 358 359 360 361 " 362 363 364 365 366 }; 367 368 369 370 371 int 372 373 374 375 376 []to 377 378 379 380 381 = 382 383 384 385 386 {R.id.img,R.id.txtName,R.id.txtLength}; SimpleAdapter adapter 387 388 389 390 391 = 392 393 394 395 396 new 397 398 399 400 401 SimpleAdapter( 402 403 404 405 406 this 407 408 409 410 411 ,lists, R.layout.image, from, to); listView.setAdapter(adapter); 412 413 414 415 416 417 418 419
看看运行截图吧:
