亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

安卓ListView中CheckBox的使用(支持Item列表項的刪除,全選,全不選)

發布時間:2020-07-19 15:31:05 來源:網絡 閱讀:1294 作者:uxlwsfmf 欄目:移動開發
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:fadingEdge="none" />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="40.0dip"
        android:layout_alignParentBottom="true" >
        <CheckBox
            android:id="@+id/all_check_btn"
            android:layout_width="40.0dip"
            android:layout_height="40.0dip"
            android:layout_alignParentLeft="true"
             />
    </RelativeLayout>
</RelativeLayout>




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="3.0dip"
    android:layout_weight="1.0"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal" >
    <CheckBox
        android:id="@+id/isCheakBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" />
    <!-- 日報圖片 -->
    <ImageView
        android:id="@+id/dailyPic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginTop="3.0dip"
        android:layout_toRightOf="@id/isCheakBox"
        android:contentDescription="dailyPic"
        />
    <!-- 附件名稱 -->
    <TextView
        android:id="@+id/dailyName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/dailyPic"
        android:text="日報名稱"
        android:textColor="#000000"
        android:textSize="12.0sp" />
    <ImageButton
        android:id="@+id/deleteAttachment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:layout_marginTop="3.0dip"
        android:focusable="false" />
    <!-- 附件名稱 -->
</RelativeLayout>
public class ListViewCheckBoxActivity extends ListActivity {
     private static final String TAG = "ListViewCheckBoxActivity"; 
       
        private List<Item> itemList; 
        private DraftDailyAdapter adapter; 
        private Map<Integer, Boolean> 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<Item>(); 
            isCheckedMap = new HashMap<Integer, Boolean>(); 
            //初始化數據 
            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<Integer> set = isCheckedMap.keySet(); 
                    Iterator<Integer> 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<Item> list; 
            private Context context; 
            LayoutInflater inflater; 
        
            public DraftDailyAdapter(Context context, List<Item> 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; 
        } 
            
    }


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

正安县| 南江县| 浮梁县| 安溪县| 眉山市| 汕尾市| 红安县| 凌源市| 二手房| 岗巴县| 溧水县| 张北县| 上思县| 嘉定区| 永胜县| 墨江| 莱芜市| 甘谷县| 襄城县| 华蓥市| 客服| 翁牛特旗| 保靖县| 开远市| 麻栗坡县| 宜良县| 望都县| 肥西县| 山东省| 贵州省| 政和县| 眉山市| 安仁县| 淮安市| 容城县| 子洲县| 聂拉木县| 新余市| 芮城县| 湟中县| 富锦市|