安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)



    
    
        
    

成都创新互联公司是一家专注于成都网站设计、网站制作、外贸营销网站建设与策划设计,肃南裕固族自治网站建设哪家好?成都创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:肃南裕固族自治等地区。肃南裕固族自治做网站价格咨询:028-86922220



    
    
    
    
    
    
    
public class ListViewCheckBoxActivity extends ListActivity {
     private static final String TAG = "ListViewCheckBoxActivity"; 
       
        private List itemList; 
        private DraftDailyAdapter adapter; 
        private Map isCheckedMap; 
        private CheckBox allCheckBox; 
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
            allCheckBox = (CheckBox)findViewById(R.id.all_check_btn); 
            itemList = new ArrayList(); 
            isCheckedMap = new HashMap(); 
            //初始化数据 
            for(int i=0;i<8;i++){ 
                Item item = new Item(); 
                item.id=i; 
                item.name = "第"+i+"篇日报"; 
                itemList.add(item); 
                isCheckedMap.put(i,false); 
            } 
                
            adapter = new DraftDailyAdapter(this,itemList); 
            setListAdapter(adapter); 
            allCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){  
                @Override  
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
                    Set set = isCheckedMap.keySet(); 
                    Iterator iterator = set.iterator();   
                    if(isChecked){  
                        while(iterator.hasNext()){    
                            Integer keyId = iterator.next();    
                            isCheckedMap.put(keyId,true); 
                        }    
                    }else{  
                        while(iterator.hasNext()){    
                            Integer keyId = iterator.next();   
                            isCheckedMap.put(keyId,false); 
                        }   
                    } 
                    adapter.notifyDataSetChanged(); 
                }  
            });  
        } 
                
        class DraftDailyAdapter extends BaseAdapter { 
        
            public List list; 
            private Context context; 
            LayoutInflater inflater; 
        
            public DraftDailyAdapter(Context context, List list) { 
                super(); 
                this.list = list; 
                this.context = context; 
                inflater = LayoutInflater.from(this.context); 
            } 
            @Override 
            public int getCount() { 
                return list == null ? 0 : list.size(); 
            } 
            @Override 
            public Object getItem(int location) { 
                return list.get(location); 
            } 
            @Override 
            public long getItemId(int position) { 
                return position; 
            } 
            @Override   
            public View getView(int position, View convertView, ViewGroup parent) {   
                ViewHolder holder = null;     
                Item item = list.get(position); 
                //Item的位置 
                final int listPosition = position; 
                //这个记录item的id用于操作isCheckedMap来更新CheckBox的状态 
                final int id = item.id; 
                if(convertView == null){ 
                    holder = new ViewHolder(); 
                    convertView = inflater.inflate(R.layout.item, null);   
                    holder.tvName = (TextView)convertView.findViewById(R.id.dailyName);   
                    holder.deleteButton = (ImageButton)convertView.findViewById(R.id.deleteAttachment); 
                    holder.cBox = (CheckBox)convertView.findViewById(R.id.isCheakBox); 
                    convertView.setTag(holder); 
                }else{ 
                    holder = (ViewHolder) convertView.getTag(); 
                } 
                Log.d(TAG, "id="+id); 
                holder.cBox.setChecked(isCheckedMap.get(id)); 
                holder.tvName.setText(item.name);  
                holder.deleteButton.setOnClickListener(new OnClickListener() { 
                    @Override 
                    public void onClick(View paramView) { 
                        //Log.d(TAG, "deletePosition="+listPosition+""); 
                        //删除list中的数据 
                        list.remove(listPosition); 
                        //删除Map中对应选中状态数据 
                        isCheckedMap.remove(id); 
                        //通知列表数据修改 
                        adapter.notifyDataSetChanged(); 
                    } 
                }); 
                holder.cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){  
                    @Override  
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
                        if(isChecked){  
                            isCheckedMap.put(id,true); 
                        }else{  
                            isCheckedMap.put(id,false); 
                        } 
                    }  
                });  
                return convertView;   
            } 
            public final class ViewHolder {     
                public TextView tvName;     
                public ImageButton deleteButton;     
                public CheckBox cBox;     
            }     
        } 
        
        class Item { 
            private Integer id; 
            private String name; 
        } 
            
    }

当前题目:安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)
当前链接:http://myzitong.com/article/giicpp.html