android ListView中Checkbox实现单选,全选,全不选功能

 
@Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.group_list2);
  
  initBottomBar();
  
  listView = (ListView) findViewById(R.id.groupListView);
  
  GroupService service = new GroupService(this);
  groupList = service.getGroupList(null);
  adapter = new GroupListAdapter(this, groupList);
  listView.setAdapter(adapter);
  
  listView.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int position,
     long arg3) {
    // TODO Auto-generated method stub
    Group group = groupList.get(position);
    CheckBox ctb = (CheckBox) view.findViewById(R.id.checkBox);
    ctb.toggle();
    adapter.selectedMap.put(group.id, ctb.isChecked());
    adapter.notifyDataSetChanged();
    if(adapter.selectedMap.containsValue(true)){
     add2ScanListBtn.setEnabled(true);
    }else{
     add2ScanListBtn.setEnabled(false);
    }
   }
  });
  
 }

 

 private  void initBottomBar(){
  bottomBarView = (LinearLayout) findViewById(R.id.bottomBar);
  add2ScanListBtn = (Button) bottomBarView.findViewById(R.id.add2ScanListBtn);
  add2ScanListBtn.setEnabled(false);
  checkBtn = (Button) bottomBarView.findViewById(R.id.checkBtn);
  closeCheckBtn = (Button) bottomBarView.findViewById(R.id.closeCheckBtn);
  
  add2ScanListBtn.setOnClickListener(this);
  checkBtn.setOnClickListener(this);
  closeCheckBtn.setOnClickListener(this);
 }
@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_EDITOR, 0, "编辑");
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_EDITOR:
			setCheckBoxVisible(View.VISIBLE);
			adapter.notifyDataSetChanged();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.add2ScanListBtn:
			GroupService service = new GroupService(this);
			service.add2ScanList(adapter.selectedMap);
			for (Group group : groupList) {
				adapter.selectedMap.put(group.id, false);
			}
			setCheckBoxVisible(View.GONE);
			adapter.notifyDataSetChanged();
			break;
		case R.id.checkBtn:
			CharSequence text = checkBtn.getText();
			if (getText(R.string.checkAll).equals(text)) {
				for (int i = 0; i < adapter.getCount(); i++) {
					Group group = groupList.get(i);
					adapter.selectedMap.put(group.id, true);
				}
				checkBtn.setText(getText(R.string.checkNeither));
				add2ScanListBtn.setEnabled(true);
			}else if(getText(R.string.checkNeither).equals(text)){
				for (int i = 0; i < adapter.getCount(); i++) {
					Group group = groupList.get(i);
					adapter.selectedMap.put(group.id, false);
				}
				checkBtn.setText(getText(R.string.checkAll));
				add2ScanListBtn.setEnabled(false);
			}
			
			adapter.notifyDataSetChanged();
			break;
		case R.id.closeCheckBtn:
			setCheckBoxVisible(View.GONE);
			adapter.notifyDataSetChanged();
			break;
		default:
			break;
		}
	}

 

//设置checkbox是否可见
	private void setCheckBoxVisible(int state){
		bottomBarView.setVisibility(state);
		for (int i = 0; i < adapter.getCount(); i++) {
			Group group = groupList.get(i);
			adapter.visibleMap.put(group.id, state);
		}
	} 


class GroupListAdapter extends BaseAdapter{
		
		private Context context;
		private List<Group> groupList;
		private Map<Integer, Boolean> selectedMap;//保存checkbox是否被选中的状态
		private Map<Integer, Integer> visibleMap ;
		public GroupListAdapter(Context context, List<Group> groupList) {
			this.context = context;
			this.groupList = groupList;
			selectedMap = new HashMap<Integer, Boolean>();
			visibleMap = new HashMap<Integer, Integer>();
			for (Group group : groupList) {
				selectedMap.put(group.id, false);
				visibleMap.put(group.id, View.GONE);
			}
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return groupList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.group_list2_item, null);
				holder.groupNameCTV = (TextView) convertView.findViewById(R.id.groupNameTV);
				holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Group group = groupList.get(position);
			holder.groupNameCTV.setText(group.m_strGroupName);
			holder.checkBox.setVisibility(visibleMap.get(group.id));
			holder.checkBox.setChecked(selectedMap.get(group.id));
			return convertView;
		}
		
		private class ViewHolder{
			TextView groupNameCTV;
			CheckBox checkBox;
		}
	}


group_list2.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">
  	  <LinearLayout android:layout_height="0dip"
	  	android:layout_width="match_parent"
	  	android:layout_weight="1">
	  	<ListView android:id="@+id/groupListView"
	  		android:layout_width="match_parent"
	  		android:layout_height="match_parent"
	  		android:divider="#999999"
	  		android:dividerHeight="1sp"
	  		/>
  		</LinearLayout>
  		
  		<LinearLayout android:id="@+id/bottomBar"
  			android:layout_height="wrap_content"
		  	android:layout_width="match_parent" android:gravity="center_horizontal"
		  	android:padding="5dip" android:background="@drawable/buttonbar"
		  	android:visibility="gone">
		  	<Button android:id="@+id/add2ScanListBtn"
			    android:layout_width="wrap_content"
			    android:layout_height="wrap_content" 
			    android:text="@string/add2ScanList"/>
		    <Button android:id="@+id/checkBtn"
			    android:layout_width="wrap_content"
			    android:layout_height="wrap_content"
			    android:text="@string/checkAll" />
			<Button android:id="@+id/closeCheckBtn"
			    android:layout_width="wrap_content"
			    android:layout_height="wrap_content"
			    android:text="@string/cancel" />
  		</LinearLayout>
</LinearLayout>


group_list2_item.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  >
  
  		<TextView android:id="@+id/groupNameTV"
		  	android:layout_width="wrap_content"
		  	android:layout_height="wrap_content"
		  	android:textSize="20dip"
		  	android:layout_alignParentLeft="true"
		  	android:textColor="@android:color/white"
		  	android:layout_marginTop="12dip"
		  	android:layout_marginLeft="5dip"
		  	android:paddingBottom="12dip"
		  	/>

		<CheckBox android:id="@+id/checkBox"
			android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	  		android:layout_alignParentRight="true"
	  		android:layout_marginRight="5dip"
	  		android:clickable="false"
		  	android:focusable="false"
		  	android:focusableInTouchMode="false"
	    />
  
</RelativeLayout>


 

 

 


你可能感兴趣的:(android,object,ListView,null,Integer,Class)