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

网友投稿 1297 2022-11-07

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

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 groupList; private Map selectedMap;//保存checkbox是否被选中的状态 private Map visibleMap ; public GroupListAdapter(Context context, List groupList) { this.context = context; this.groupList = groupList; selectedMap = new HashMap(); visibleMap = new HashMap(); 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

上一篇:用ExpandableListView实现类似QQ好友列表
下一篇:Ubuntu10.10(64位)编译Android2.3源码问题解决
相关文章

 发表评论

暂时没有评论,来抢沙发吧~