Android中ListView结合CheckBox判断选中项


本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作。通过一个Demo来展示该功能,选中ListView中的某一项,然后点击Button按钮来显示选中了哪些项。

[1] 程序结构图如下:


其中Person.java是实体类,MainActivity.java是Activity组件类。listitem.xml是自定义的列表每项布局文件。

[2] listitem.xml布局文件源码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:Android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent">  
  7.   <LinearLayout  
  8.      android:layout_width="fill_parent"  
  9.      android:layout_height="wrap_content"  
  10.      android:orientation="horizontal"  
  11.      android:descendantFocusability="blocksDescendants">  
  12.       <CheckBox  
  13.          android:id="@+id/list.select"  
  14.          android:layout_width="wrap_content"  
  15.          android:layout_height="wrap_content"/>  
  16.       <TextView  
  17.          android:id="@+id/list.name"  
  18.          android:layout_width="fill_parent"  
  19.          android:layout_height="wrap_content"  
  20.          android:layout_weight="1"  
  21.          android:text="Name"  
  22.          android:layout_gravity="center"  
  23.          android:textSize="20dp"  
  24.          android:layout_marginLeft="10dp"/>  
  25.       <TextView  
  26.          android:id="@+id/list.address"  
  27.          android:layout_width="fill_parent"  
  28.          android:layout_height="wrap_content"  
  29.          android:layout_weight="1"  
  30.          android:text="Address"  
  31.          android:layout_gravity="center"  
  32.          android:textSize="20dp"/>  
  33.   </LinearLayout>  
  34. </LinearLayout>  
[3] main.xml布局文件源码如下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <Button  
  7.        android:id="@+id/show"  
  8.        android:layout_width="fill_parent"  
  9.        android:layout_height="wrap_content"  
  10.        android:text="Show"/>  
  11.     <ListView  
  12.        android:id="@+id/lvperson"  
  13.        android:layout_width="fill_parent"  
  14.        android:layout_height="fill_parent"/>  
  15. </LinearLayout>  
[4] Person.java实体类源码如下:
  1. package com.andyidea.bean;  
  2.   
  3. public class Person {  
  4.   
  5.     private String name;  
  6.     private String address;  
  7.       
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.     public void setName(String name) {  
  12.         this.name = name;  
  13.     }  
  14.     public String getAddress() {  
  15.         return address;  
  16.     }  
  17.     public void setAddress(String address) {  
  18.         this.address = address;  
  19.     }  
  20.       
  21. }  
  • 1
  • 2
  • 下一页

相关内容