Android Listview与Checkbox结合实现选择,删除,显示选择结果

  1. idget.TextView;  
  2. import android.widget.Toast;  
  3.   
  4. public class MainActivity extends Activity {  
  5.       
  6.     Button show;  
  7.     Button select;  
  8.     Button deselect;  
  9.     ListView lv;  
  10.     Context mContext;  
  11.     MyListAdapter adapter;  
  12.   
  13.     List<Integer> selected = new ArrayList<Integer>();  
  14.     private List<Item> items;  
  15.       
  16.       
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         //mContext = getApplicationContext();  
  23.         mContext = MainActivity.this;  
  24.         select = (Button)findViewById(R.id.select);  
  25.         deselect = (Button)findViewById(R.id.deselect);  
  26.         show = (Button)findViewById(R.id.show);  
  27.         lv = (ListView)findViewById(R.id.lv);  
  28.           
  29.         items = new ArrayList<Item>();  
  30.           
  31.         for(int i=0;i< 50;i++){  
  32.             Item item = new Item();  
  33.             item.name = "wxz"+i;  
  34.             item.address = "ZhengZhou";  
  35.             item.checked = false;  
  36.             items.add(item);  
  37.       }  
  38.           
  39.         adapter = new MyListAdapter(items);  
  40.         lv.setAdapter(adapter);  
  41.           
  42.         lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener(){  
  43.   
  44.             @Override  
  45.             public void onItemClick(AdapterView<?> parent, View view, int position,  
  46.                     long id) {  
  47.                 // TODO Auto-generated method stub  
  48.                 items.get(position).checked = !items.get(position).checked;  
  49.                 adapter.notifyDataSetChanged();    
  50.                 Toast.makeText(mContext, "单击:"+items.get(position).name +",id:"+id, Toast.LENGTH_SHORT).show();  
  51.                   
  52.             }  
  53.               
  54.         });  
  55.           
  56.         select.setOnClickListener(new View.OnClickListener() {  
  57.               
  58.             @Override  
  59.             public void onClick(View v) {  
  60.                 // TODO Auto-generated method stub  
  61.                 int k = items.size();  
  62.                 for(int i=0; i<k; i++){  
  63.                      items.get(i).checked = true;     
  64.                 }     
  65.                 adapter.notifyDataSetChanged();  
  66.             }  
  67.         });  
  68.           
  69.         deselect.setOnClickListener(new View.OnClickListener() {  
  70.               
  71.             @Override  
  72.             public void onClick(View v) {  
  73.                 // TODO Auto-generated method stub  
  74.                 int k = items.size();  
  75.                 for(int i=0; i<k; i++){  
  76.                      items.get(i).checked = false;    
  77.                 }     
  78.                 adapter.notifyDataSetChanged();  
  79.             }  
  80.         });  
  81.           
  82.         show.setOnClickListener(new View.OnClickListener() {  
  83.   
  84.             @Override  
  85.             public void onClick(View v) {  
  86.                   
  87.                 selected.clear();  
  88.                 int k = items.size();  
  89.                   
  90.                 for(int i=0; i<k; i++){  
  91.                     if(items.get(i).checked){  
  92.                         selected.add(i);  
  93.                     }  
  94.                 }  
  95.                 k = selected.size();  
  96.                   
  97.                 if(k ==0){  
  98.                     AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);  
  99.                     builder1.setMessage("没有选中任何记录");  
  100.                     builder1.show();  
  101.   
  102.                 }else{  
  103.                     StringBuilder sb = new StringBuilder();  
  104.                      
  105.                     for(int i=0; i<k; i++){  
  106.                         int j = selected.get(i);  
  107.                         sb.append("ID="+(j)+" Name ="+items.get(j).name+"\n");  
  108.                     }  
  109.                     AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);  
  110.                     builder2.setMessage(sb.toString());  
  111.                     builder2.show();  
  112.                 }  
  113.             }  
  114.         });  
  115.     }  
  116.   
  117.       
  118.     //自定义ListView适配器  
  119.     class MyListAdapter extends BaseAdapter{  
  120.           
  121.            //private Context context;  
  122.         LayoutInflater inflater;  
  123.         public List<Item> items;  
  124.         public MyListAdapter(List<Item> items){  
  125.             this.items = items;  
  126.               
  127.         inflater = LayoutInflater.from(mContext);  
  128.               
  129.         }  
  130.         @Override  
  131.         public int getCount() {  
  132.             // 返回值控制该Adapter将会显示多少个列表项  
  133.             return items == null ? 0 : items.size();  
  134.         }  
  135.   
  136.         @Override  
  137.         public Object getItem(int position) {  
  138.             // 返回值决定第position处的列表项的内容  
  139.             return items.get(position);  
  140.         }  
  141.   
  142.         @Override  
  143.         public long getItemId(int position) {  
  144.             // 返回值决定第position处的列表项的ID  
  145.             return position;  
  146.         }  
  147.   
  148.         @Override  
  149.         public View getView(final int position, View convertView, ViewGroup parent) {  
  150.             ViewHolder holder = null;  
  151.             Item item = items.get(position);  
  152.             if (convertView == null) {  
  153.                 Log.e("MainActivity","position1 = "+position);  
  154.                 convertView = inflater.inflate(R.layout.listitem, null);  
  155.                   
  156.                 holder = new ViewHolder();  
  157.                 holder.btnDel = (Button)convertView.findViewById(R.id.btnDel);  
  158.                 holder.cbCheck = (CheckBox)convertView.findViewById(R.id.cbCheck);  
  159.                 holder.tvName = (TextView)convertView.findViewById(R.id.tvName);  
  160.                 holder.tvAddress = (TextView)convertView.findViewById(R.id.tvAddress);  
  161.                 holder.tvName.setText(item.name);  
  162.                 holder.tvAddress.setText(item.address);  
  163.                   
  164.                 convertView.setTag(holder);  
  165.                   
  166.             }else{  
  167.                 Log.e("MainActivity","position2 = "+position);  
  168.                 holder = (ViewHolder)convertView.getTag();  
  169.                 holder.cbCheck.setChecked(item.checked);  
  170.                 holder.tvName.setText(item.name);  
  171.                 holder.tvAddress.setText(item.address);  
  172.                 
  173.             }  
  174.             holder.btnDel.setOnClickListener(new OnClickListener(){  
  175.   
  176.                 @Override  
  177.                 public void onClick(View v) {  
  178.                     // TODO Auto-generated method stub  
  179.                       //删除list中的数据  
  180.                     items.remove(position);  
  181.                     //通知列表数据修改  
  182.                     adapter.notifyDataSetChanged();  
  183.                 }  
  184.                   
  185.             });  
  186.             holder.cbCheck.setOnClickListener(new View.OnClickListener() {  
  187.                   
  188.                 @Override  
  189.                 public void onClick(View v) {  
  190.                     CheckBox cb = (CheckBox)v;  
  191.                     items.get(position).checked = cb.isChecked();   
  192.           
  193.                 }  
  194.             });  
  195.               return convertView;    
  196.         }  
  197.           
  198.     }  
  199.       
  200.     static class ViewHolder{  
  201.         public CheckBox cbCheck;  
  202.         public TextView tvName;  
  203.         public TextView tvAddress;  
  204.         public Button btnDel;  
  205.           
  206.     }  
  207.       
  208.     class Item {  
  209.         private String name;  
  210.         private String address;  
  211.         private Boolean checked;  
  212.     }  
  213. }                                                                                                                                                                                              

 列表项listitem.xml

[html]  view plain copy
  1. <?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="wrap_content"  
  5.     android:descendantFocusability="blocksDescendants"  
  6.     android:orientation="horizontal" >  
  7.   
  8.     <CheckBox  
  9.         android:id="@+id/cbCheck"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_marginTop="5dp"  
  13.         android:focusable="false" />  
  14.   
  15.     <ImageView  
  16.         android:id="@+id/ivPhoto"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:src="@drawable/ic_launcher"/>  
  20.   
  21.     <TextView  
  22.         android:id="@+id/tvName"  
  23.         android:layout_width="fill_parent"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_marginTop="5dp"  
  26.         android:layout_weight="1"  
  27.         android:text="Name"  
  28.         android:textSize="18sp" />  
  29.   
  30.     <TextView  
  31.         android:id="@+id/tvAddress"  
  32.         android:layout_width="fill_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:layout_marginTop="5dp"  
  35.         android:layout_weight="1"  
  36.         android:text="Address"  
  37.         android:textSize="18sp" />  
  38.   
  39.     <Button  
  40.         android:id="@+id/btnDel"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="40dp"  
  43.         android:layout_marginRight="16dp"  
  44.         android:layout_marginTop="5dp"  
  45.         android:focusable="false"  
  46.         android:text="删除"  
  47.         android:textSize="16sp" />  
  48.   
  49. </LinearLayout>  

main.xml

[html]  view plain copy
  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.     <LinearLayout  
  7.         android:orientation="horizontal"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content" >  
  10.         <Button   
  11.             android:id="@+id/select"  
  12.             android:layout_width="100dp"  
  13.             android:layout_height="wrap_content"  
  14.             android:text="Select"/>  
  15.         <Button  
  16.             android:id="@+id/deselect"   
  17.             android:layout_width="100dp"  
  18.             android:layout_height="wrap_content"  
  19.             android:text="Deselect" />  
  20.         <Button  
  21.          android:id="@+id/show"  
  22.          android:layout_width="100dp"  
  23.          android:layout_height="wrap_content"  
  24.          android:text="Show"/>  
  25.           
  26.     </LinearLayout>  
  27.     
  28.     <ListView  
  29.        android:id="@+id/lv"  
  30.        android:layout_width="fill_parent"  
  31.        android:layout_height="fill_parent"  
  32.        android:fastScrollEnabled="true"  
  33.        android:fastScrollAlwaysVisible="true"/>  
  34. </LinearLayout>  

源码:http://download.csdn.net/detail/xinzheng_wang/5973407

from:http://blog.csdn.net/xinzheng_wang/article/details/10064563

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值