带选项框的listview


布局包括主界面和Listview的布局:

  1. <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.         <TextView android:id="@+id/item_tv"  
  7.             android:layout_width="0dp"  
  8.             android:layout_height="wrap_content"  
  9.             android:layout_weight="1"  
  10.             android:gravity="center_vertical"/>  
  11.         <CheckBox android:id="@+id/item_cb"  
  12.             android:layout_width="wrap_content"  
  13.             android:layout_height="wrap_content"  
  14.             android:clickable="false"  
  15.             android:focusable="false"  
  16.             android:focusableInTouchMode="false"  
  17.             android:gravity="center_vertical"/>  
  18. </LinearLayout></span>  

  1. <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <LinearLayout android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content"  
  7.         android:orientation="vertical">  
  8.         <TextView android:id="@+id/tv"  
  9.             android:layout_width="fill_parent"  
  10.             android:layout_height="wrap_content"/>  
  11.          <ListView android:id="@+id/lv"  
  12.          android:layout_width="fill_parent"  
  13.             android:layout_height="wrap_content"/>  
  14.     </LinearLayout>  
  15.       
  16.     <LinearLayout android:orientation="horizontal"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_alignParentBottom="true"  
  20.         android:background="#000000">  
  21.         <Button android:id="@+id/bt_selectall"  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"  
  24.             android:text="@string/selectall"/>  
  25.           
  26.         <Button android:id="@+id/bt_cancelselectall"  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="@string/cancelall"/>  
  30.                 
  31.         <Button android:id="@+id/bt_deselectall"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="wrap_content"  
  34.             android:text="@string/deselectall"/>  
  35.     </LinearLayout>  
  36. </RelativeLayout></span>  

然后在代码里控制:
  1. <span style="font-size:16px;">package com.kingfly;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import com.kingfly.MyAdapter.ViewHolder;  
  6.   
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.AdapterView;  
  12. import android.widget.AdapterView.OnItemClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.ListView;  
  15. import android.widget.TextView;  
  16.   
  17. public class ListViewWithCheckboxActivity extends Activity {  
  18.     /** Called when the activity is first created. */  
  19.     private ListView lv;  
  20.     private MyAdapter mAdapter;  
  21.     private ArrayList<String> list;  
  22.     private Button bt_selectall;  
  23.     private Button bt_cancel;  
  24.     private Button bt_deselectall;  
  25.     private int checkNum;  
  26.     private TextView tv_show;  
  27.   
  28.     @Override  
  29.     public void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.         lv = (ListView) findViewById(R.id.lv);  
  33.         bt_selectall = (Button) findViewById(R.id.bt_selectall);  
  34.         bt_cancel = (Button) findViewById(R.id.bt_cancelselectall);  
  35.         bt_deselectall = (Button) findViewById(R.id.bt_deselectall);  
  36.   
  37.         tv_show = (TextView) findViewById(R.id.tv);  
  38.          tv_show.setText("您已选中0项");    
  39.         list = new ArrayList<String>();  
  40.         initDate();  
  41.         mAdapter = new MyAdapter(list, this);  
  42.         lv.setAdapter(mAdapter);  
  43.         bt_selectall.setOnClickListener(new OnClickListener() {  
  44.   
  45.             public void onClick(View v) {  
  46.                 // TODO Auto-generated method stub  
  47.                 for (int i = 0; i < list.size(); i++) {  
  48.                     MyAdapter.getIsSelected().put(i, true);  
  49.                 }  
  50.                 checkNum = list.size();  
  51.                 dataChanged();  
  52.             }  
  53.         });  
  54.   
  55.         bt_cancel.setOnClickListener(new OnClickListener() {  
  56.   
  57.             public void onClick(View v) {  
  58.                 // TODO Auto-generated method stub  
  59.                 for (int i = 0; i < list.size(); i++) {  
  60.                     if (MyAdapter.getIsSelected().get(i)) {  
  61.                         MyAdapter.getIsSelected().put(i, false);  
  62.                         checkNum--;  
  63.                     }  
  64.                 }  
  65.                 dataChanged();  
  66.             }  
  67.         });  
  68.   
  69.         bt_deselectall.setOnClickListener(new OnClickListener() {  
  70.   
  71.             public void onClick(View v) {  
  72.                 // TODO Auto-generated method stub  
  73.                 for (int i = 0; i < list.size(); i++) {  
  74.                     if (MyAdapter.getIsSelected().get(i)) {  
  75.                         MyAdapter.getIsSelected().put(i, false);  
  76.                         checkNum--;  
  77.                     } else {  
  78.                         MyAdapter.getIsSelected().put(i, true);  
  79.                         checkNum++;  
  80.                     }  
  81.                 }  
  82.                 dataChanged();  
  83.             }  
  84.         });  
  85.           
  86.           
  87.           lv.setOnItemClickListener(new OnItemClickListener() {    
  88.                 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,    
  89.                         long arg3) {    
  90.                     ViewHolder holder = (ViewHolder) arg1.getTag();    
  91.                     holder.cb.toggle();    
  92.                     MyAdapter.getIsSelected().put(arg2, holder.cb.isChecked());     
  93.                     if (holder.cb.isChecked() == true) {    
  94.                         checkNum++;    
  95.                     } else {    
  96.                         checkNum--;    
  97.                     }    
  98.                     tv_show.setText("您已选中"+checkNum+"项");    
  99.                         
  100.                 }    
  101.             });    
  102.   
  103.     }  
  104.   
  105.     private void initDate() {  
  106.         for (int i = 0; i < 15; i++) {  
  107.             list.add("data" + " " + i);  
  108.         }  
  109.     }  
  110.   
  111.     private void dataChanged() {  
  112.         mAdapter.notifyDataSetChanged();  
  113.         tv_show.setText("choose:" + checkNum);  
  114.     }  
  115. }</span>  

  1. <span style="font-size:16px;">package com.kingfly;  
  2.   
  3. import java.security.PublicKey;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6.   
  7. import android.content.BroadcastReceiver;  
  8. import android.content.Context;  
  9. import android.content.Intent;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.widget.BaseAdapter;  
  14. import android.widget.CheckBox;  
  15. import android.widget.TextView;  
  16.   
  17. public class MyAdapter extends BaseAdapter {  
  18.     private ArrayList<String> list;  
  19.     private static HashMap<Integer, Boolean> isSelected;  
  20.     private Context context;  
  21.     private LayoutInflater inflater = null;  
  22.   
  23.     public MyAdapter(ArrayList<String> list, Context context) {  
  24.         this.context = context;  
  25.         this.list = list;  
  26.         inflater = LayoutInflater.from(context);  
  27.         isSelected = new HashMap<Integer, Boolean>();  
  28.         initDate();  
  29.     }  
  30.   
  31.     private void initDate() {  
  32.         for (int i = 0; i < list.size(); i++) {  
  33.             getIsSelected().put(i, false);  
  34.         }  
  35.     }  
  36.   
  37.     public int getCount() {  
  38.         // TODO Auto-generated method stub  
  39.         return list.size();  
  40.     }  
  41.   
  42.     public Object getItem(int position) {  
  43.         // TODO Auto-generated method stub  
  44.         return list.get(position);  
  45.     }  
  46.   
  47.     public long getItemId(int position) {  
  48.         // TODO Auto-generated method stub  
  49.         return position;  
  50.     }  
  51.   
  52.     public View getView(int position, View convertView, ViewGroup parent) {  
  53.         // TODO Auto-generated method stub  
  54.         ViewHolder holder = null;  
  55.         if (convertView == null) {  
  56.             holder = new ViewHolder();  
  57.             convertView = inflater.inflate(R.layout.listviewitem, null);  
  58.             holder.tv = (TextView) convertView.findViewById(R.id.item_tv);  
  59.             holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);  
  60.             convertView.setTag(holder);  
  61.         } else {  
  62.             holder = (ViewHolder) convertView.getTag();  
  63.         }  
  64.   
  65.         holder.tv.setText(list.get(position));  
  66.         holder.cb.setChecked(getIsSelected().get(position));  
  67.         return convertView;  
  68.     }  
  69.   
  70.     public static HashMap<Integer, Boolean> getIsSelected() {  
  71.         return isSelected;  
  72.     }  
  73.   
  74.     public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {  
  75.         MyAdapter.isSelected = isSelected;  
  76.     }  
  77.       
  78.     class ViewHolder{  
  79.         TextView tv;  
  80.   
  81.         CheckBox cb;  
  82.     }  
  83.   
  84. }  
  85. </span>  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.kingfly"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="15" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:name=".ListViewWithCheckboxActivity"  
  14.             android:label="@string/app_name" >  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.   
  23. </manifest> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值