本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作。通过一个Demo来展示该功能,选中ListView中的某一项,然后点击Button按钮来显示选中了哪些项。
[1] 程序结构图如下:
其中Person.java是实体类,MainActivity.java是Activity组件类。listitem.xml是自定义的列表每项布局文件。
[2] listitem.xml布局文件源码如下:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
03 | xmlns:android = "http://schemas.android.com/apk/res/android" |
04 | android:orientation = "vertical" |
05 | android:layout_width = "fill_parent" |
06 | android:layout_height = "fill_parent" > |
08 | android:layout_width = "fill_parent" |
09 | android:layout_height = "wrap_content" |
10 | android:orientation = "horizontal" |
11 | android:descendantFocusability = "blocksDescendants" > |
13 | android:id = "@+id/list.select" |
14 | android:layout_width = "wrap_content" |
15 | android:layout_height = "wrap_content" /> |
17 | android:id = "@+id/list.name" |
18 | android:layout_width = "fill_parent" |
19 | android:layout_height = "wrap_content" |
20 | android:layout_weight = "1" |
22 | android:layout_gravity = "center" |
23 | android:textSize = "20dp" |
24 | android:layout_marginLeft = "10dp" /> |
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" /> |
[3] main.xml布局文件源码如下:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" > |
07 | android:id = "@+id/show" |
08 | android:layout_width = "fill_parent" |
09 | android:layout_height = "wrap_content" |
12 | android:id = "@+id/lvperson" |
13 | android:layout_width = "fill_parent" |
14 | android:layout_height = "fill_parent" /> |
[4] Person.java实体类源码如下:
01 | package com.andyidea.bean; |
06 | private String address; |
08 | public String getName() { |
11 | public void setName(String name) { |
14 | public String getAddress() { |
17 | public void setAddress(String address) { |
18 | this .address = address; |
[5] MainActivity.java类源码如下:
001 | package com.andyidea.listview; |
003 | import java.util.ArrayList; |
004 | import java.util.HashMap; |
005 | import java.util.List; |
007 | import com.andyidea.bean.Person; |
009 | import android.app.Activity; |
010 | import android.app.AlertDialog; |
011 | import android.content.Context; |
012 | import android.os.Bundle; |
013 | import android.util.Log; |
014 | import android.view.LayoutInflater; |
015 | import android.view.View; |
016 | import android.view.ViewGroup; |
017 | import android.widget.BaseAdapter; |
018 | import android.widget.Button; |
019 | import android.widget.CheckBox; |
020 | import android.widget.ListView; |
021 | import android.widget.TextView; |
023 | public class MainActivity extends Activity { |
027 | List<Person> persons = new ArrayList<Person>(); |
029 | MyListAdapter adapter; |
030 | List<Integer> listItemID = new ArrayList<Integer>(); |
032 | /** Called when the activity is first created. */ |
034 | public void onCreate(Bundle savedInstanceState) { |
035 | super .onCreate(savedInstanceState); |
036 | setContentView(R.layout.main); |
037 | mContext = getApplicationContext(); |
038 | show = (Button)findViewById(R.id.show); |
039 | lv = (ListView)findViewById(R.id.lvperson); |
042 | adapter = new MyListAdapter(persons); |
043 | lv.setAdapter(adapter); |
045 | show.setOnClickListener( new View.OnClickListener() { |
048 | public void onClick(View v) { |
051 | for ( int i= 0 ;i<adapter.mChecked.size();i++){ |
052 | if (adapter.mChecked.get(i)){ |
057 | if (listItemID.size()== 0 ){ |
058 | AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity. this ); |
059 | builder1.setMessage( "没有选中任何记录" ); |
062 | StringBuilder sb = new StringBuilder(); |
064 | for ( int i= 0 ;i<listItemID.size();i++){ |
065 | sb.append( "ItemID=" +listItemID.get(i)+ " . " ); |
067 | AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity. this ); |
068 | builder2.setMessage(sb.toString()); |
078 | private void initPersonData(){ |
080 | for ( int i= 1 ;i<= 12 ;i++){ |
081 | mPerson = new Person(); |
082 | mPerson.setName( "Andy" +i); |
083 | mPerson.setAddress( "GuangZhou" +i); |
084 | persons.add(mPerson); |
089 | class MyListAdapter extends BaseAdapter{ |
090 | List<Boolean> mChecked; |
091 | List<Person> listPerson; |
092 | HashMap<Integer,View> map = new HashMap<Integer,View>(); |
094 | public MyListAdapter(List<Person> list){ |
095 | listPerson = new ArrayList<Person>(); |
098 | mChecked = new ArrayList<Boolean>(); |
099 | for ( int i= 0 ;i<list.size();i++){ |
105 | public int getCount() { |
106 | return listPerson.size(); |
110 | public Object getItem( int position) { |
111 | return listPerson.get(position); |
115 | public long getItemId( int position) { |
120 | public View getView( int position, View convertView, ViewGroup parent) { |
122 | ViewHolder holder = null ; |
124 | if (map.get(position) == null ) { |
125 | Log.e( "MainActivity" , "position1 = " +position); |
127 | LayoutInflater mInflater = (LayoutInflater) mContext |
128 | .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
129 | view = mInflater.inflate(R.layout.listitem, null ); |
130 | holder = new ViewHolder(); |
131 | holder.selected = (CheckBox)view.findViewById(R.id.list_select); |
132 | holder.name = (TextView)view.findViewById(R.id.list_name); |
133 | holder.address = (TextView)view.findViewById(R.id.list_address); |
134 | final int p = position; |
135 | map.put(position, view); |
136 | holder.selected.setOnClickListener( new View.OnClickListener() { |
139 | public void onClick(View v) { |
140 | CheckBox cb = (CheckBox)v; |
141 | mChecked.set(p, cb.isChecked()); |
146 | Log.e( "MainActivity" , "position2 = " +position); |
147 | view = map.get(position); |
148 | holder = (ViewHolder)view.getTag(); |
151 | holder.selected.setChecked(mChecked.get(position)); |
152 | holder.name.setText(listPerson.get(position).getName()); |
153 | holder.address.setText(listPerson.get(position).getAddress()); |
160 | static class ViewHolder{ |
[6] 程序运行后的结果如下:
文章出处:http://blog.csdn.net/cjjky/article/details/6967219