欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码

程序员文章站 2023-12-21 20:38:46
最近在项目开发中,由于项目的需求要实现一些列表的单选,多选,全选,批量输入之类的功能,其实功能的实现倒不是很复杂,需求中也没有涉及到复杂的动画什么之类,主要是解决列表数据复...

最近在项目开发中,由于项目的需求要实现一些列表的单选,多选,全选,批量输入之类的功能,其实功能的实现倒不是很复杂,需求中也没有涉及到复杂的动画什么之类,主要是解决列表数据复用的问题,解决好这个就可以了。下面是最近项目中涉及到的一些:

listview实现多选、全选、取消全选:

下面是适配器,一开始在适配器的构造函数中,对数据进行初始化,同时定义一个集合用于管理listview的点击;

class batchadpter extends baseadapter {
 private hashmap<integer, boolean> isselected;
 private list<dtgzscustomer> list;
 private context context;
 @suppresslint("usesparsearrays")
 public batchadpter(list<dtgzscustomer> list,context context) {
 this.context=context;
 this.list = new arraylist<dtgzscustomer>();
 if (list != null) {
 this.list.addall(list);
 }
 isselected = new hashmap<integer, boolean>();
 initdate(false);
 }
 // 初始化isselected的数据
 private void initdate(boolean bool) {
 for (int i = 0; i < list.size(); i++) {
 dtgzscustomer dtgzscustomer = list.get(i);
 if (bool) {  
  datas.add(dtgzscustomer.thread_id);
 } else {
  datas.remove(dtgzscustomer.thread_id);
 }
 getisselected().put(i, bool);
 }
 }
 public hashmap<integer, boolean> getisselected() {
 return isselected;
 }
 public void setisselected(hashmap<integer, boolean> isselected) {
 this.isselected = isselected;
 }
 public void nodfiydata(list<dtgzscustomer> list) {
 if (list != null) {
 this.list.clear();
 this.list.addall(list);
 }
 notifydatasetchanged();
 }
 @override
 public int getcount() {
 return list.size();
 }
 @override
 public object getitem(int position) {
 return list.get(position);
 }
 @override
 public long getitemid(int position) {
 return position;
 }
 @override
 public view getview(int position, view convertview, viewgroup parent) {
 viewholder holder;
 if (convertview == null) {
 holder = new viewholder();
 convertview = view.inflate(context, r.layout.no_contact_listview_item, null);
 holder.name = (textview) convertview.findviewbyid(r.id.name);
 holder.sex = (textview) convertview.findviewbyid(r.id.sex);
 holder.popuse = (textview) convertview.findviewbyid(r.id.popuse);
 holder.tv_phone = (textview) convertview.findviewbyid(r.id.tv_phone);
 holder.allocation = (textview) convertview.findviewbyid(r.id.allocation);
 holder.time = (textview) convertview.findviewbyid(r.id.time);
 holder.btn_dis = (button) convertview.findviewbyid(r.id.btn_dis);
 holder.btn_allot = (button) convertview.findviewbyid(r.id.btn_allot);
 holder.btn_telephone = (button) convertview.findviewbyid(r.id.btn_telephone);
 holder.btn_follow = (button) convertview.findviewbyid(r.id.btn_follow);
 holder.cb = (checkbox) convertview.findviewbyid(r.id.cb);
 holder.allocation.setvisibility(view.invisible);
 convertview.settag(holder);
 } else {
 holder = (viewholder) convertview.gettag();
 }
 dtgzscustomer data = list.get(position);
 salestools.settextviewtext(holder.name, data.p_customer_name);
 salestools.settextviewtext(holder.sex, data.gender);
 salestools.settextviewtext(holder.popuse, data.purpose_series);
 salestools.settextviewtext(holder.tv_phone, data.mobile_no);
 salestools.settextviewtext(holder.allocation, data.thread_contact_state);
 salestools.settextviewtext(holder.time, data.thread_create_date);
 holder.btn_dis.setvisibility(view.invisible);
 holder.btn_allot.setvisibility(view.invisible);
 holder.btn_telephone.setvisibility(view.invisible);
 holder.btn_follow.setvisibility(view.invisible);
 holder.cb.setvisibility(view.visible);
 hashmap<integer, boolean> isselected2 = getisselected();
 boolean boolean1 = isselected2.get(position);
 if (boolean1 != null) {
 holder.cb.setchecked(boolean1);
 }
 // 初始化的时候做处理,如果管理标记的集合中没有该pos则设为false如果有则设为true
 if (adapter != null) {
 hashmap<integer, boolean> isselected = adapter.getisselected();
 if (isselected != null && isselected.containskey(position)) {
  holder.cb.setchecked(isselected.get(position));
 } else {
  holder.cb.setchecked(false);
 }
 }
 return convertview;
 }
 }
 class viewholder {
 textview name, sex, popuse, tv_phone, allocation, time;
 button btn_dis, btn_allot, btn_telephone, btn_follow;
 checkbox cb;
 }

这里是全选的方法:

// 全选
 private void choiceall() {
 adapter.initdate(true);
 // 数量设为list的长度
 checknum = list.size();
 // 刷新listview和textview的显示
 cb_all.settextcolor(color.parsecolor("#0097e0"));
 datachanged();
 }

这里是条目点击事件:

@override
 public void onitemclick(adapterview<?> parent, view view, int position, long id) {
 dtgzscustomer dtgzscustomer = list.get(position);
 // 取得viewholder对象,这样就省去了通过层层的findviewbyid去实例化我们需要的cb实例的步骤
 viewholder holder = (viewholder) view.gettag();
 // 改变checkbox的状态
 holder.cb.toggle();
 // 将checkbox的选中状况记录下来
 adapter.getisselected().put(position, holder.cb.ischecked());
 // 调整选定条目
 if (holder.cb.ischecked() == true) {
 checknum++;
 if (checknum==list.size()) {
 cb_all.setchecked(true);
 cb_all.settextcolor(color.parsecolor("#0097e0"));
 }
 datas.add(dtgzscustomer.thread_id);
 } else {
 cb_all.setchecked(false);
 cb_all.settextcolor(color.parsecolor("#c0c0c0"));
 checknum--;
 datas.remove(dtgzscustomer.thread_id);
 }
 datachanged();
 }

下面是listview和radionbutton实现单个条目单选,整个列表多线:

class checkadapter extends baseadapter {
 private context mcontext;
 private list<dtgzsschedule> list;
 public checkadapter(context context, list<dtgzsschedule> list) {
 this.mcontext = context;
 this.list=list;
 }
 @override
 public int getcount() {
 return list.size();
 }
 @override
 public object getitem(int position) {
 return list.get(position);
 }
 @override
 public long getitemid(int position) {
 return position;
 }
 @override
 public view getview(int position, view convertview, viewgroup parent) {
 viewholder holder;
 if (convertview==null) {
 holder=new viewholder();
 convertview=layoutinflater.from(mcontext).inflate(r.layout.check_listview_item, parent, false);
 holder.sale_name = (textview) convertview.findviewbyid(r.id.sale_name);
 holder.sale_branch = (textview) convertview.findviewbyid(r.id.sale_branch);
 holder.scb = (radiobutton) convertview.findviewbyid(r.id.scb);
 holder.scc = (radiobutton) convertview.findviewbyid(r.id.scc);
 holder.scd = (radiobutton) convertview.findviewbyid(r.id.scd);
 holder.sce = (radiobutton) convertview.findviewbyid(r.id.sce);
 convertview.settag(holder);
 }else{
 holder=(viewholder) convertview.gettag();
 }
 final dtgzsschedule dtgzsschedule = list.get(position);
 oncheckedchangelistener listener = new oncheckedchangelistener() {
 @override
 public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
  string choice = buttonview.gettext().tostring();
  if (choice.equals("到岗")) {
  if (ischecked == true) {
  dtgzsschedule.check_type = "0";
  setactualnum();
  }
  } else {
  if (choice.equals("迟到")) {
  if (ischecked == true) {
  dtgzsschedule.check_type = "1";
  setactualnum();
  }
  } else if (choice.equals("休假")) {
  if (ischecked == true) {
  dtgzsschedule.check_type = "2";
  setactualnum();
  }
  } else if (choice.equals("旷工")) {
  if (ischecked == true) {
  dtgzsschedule.check_type = "3";
  setactualnum();
  }
  }
  }
 }
 };
 holder.sce.setoncheckedchangelistener(listener);
 holder.scd.setoncheckedchangelistener(listener);
 holder.scc.setoncheckedchangelistener(listener);
 holder.scb.setoncheckedchangelistener(listener);
 holder.sale_name.settext("" + dtgzsschedule.sales_consultant_name);
 holder.sale_branch.settext("" + dtgzsschedule.sales_branch);
 string check_type = dtgzsschedule.check_type;
 if (check_type.equals("0")) {// 到岗
 dtgzsschedule.scbchecked = true;
 dtgzsschedule.sccchecked = false;
 dtgzsschedule.scdchecked = false;
 dtgzsschedule.scechecked = false;
 } else if (check_type.equals("1")) {// 迟到
 dtgzsschedule.scbchecked = false;
 dtgzsschedule.sccchecked = true;
 dtgzsschedule.scdchecked = false;
 dtgzsschedule.scechecked = false;
 } else if (check_type.equals("2")) {// 旷工
 dtgzsschedule.scbchecked = false;
 dtgzsschedule.sccchecked = false;
 dtgzsschedule.scdchecked = true;
 dtgzsschedule.scechecked = false;
 } else if (check_type.equals("3")) {// 休假
 dtgzsschedule.scbchecked = false;
 dtgzsschedule.sccchecked = false;
 dtgzsschedule.scdchecked = false;
 dtgzsschedule.scechecked = true;
 }
 holder.scb.setchecked(dtgzsschedule.scbchecked);
 holder.scc.setchecked(dtgzsschedule.sccchecked);
 holder.scd.setchecked(dtgzsschedule.scdchecked);
 holder.sce.setchecked(dtgzsschedule.scechecked);
 return convertview;
 }
 }
 class viewholder{
 textview sale_name, sale_branch;
 radiobutton scb, scc, scd, sce;
 }

expandablelistview实现子条目单选:

class pinnedheaderexpandableadapter extends baseexpandablelistadapter implements headeradapter {
 private context context;
 private pinnedheaderexpandablelistview listview;
 private layoutinflater inflater;
 private map<string, list<dtgzscustomer>> map;
 private list<string> parentlist;
 @suppresslint("usesparsearrays")
 public pinnedheaderexpandableadapter(list<string> parentlist, map<string, list<dtgzscustomer>> map, context context,
 pinnedheaderexpandablelistview listview) {
 this.context = context;
 this.listview = listview;
 inflater = layoutinflater.from(this.context);
 this.map = new hashmap<string, list<dtgzscustomer>>();
 if (map != null) {
 this.map.putall(map);
 }
 this.parentlist = new arraylist<string>();
 if (parentlist != null) {
 this.parentlist.addall(parentlist);
 }
 }
 public void notifymap(map<string, list<dtgzscustomer>> map) {
 if (map != null) {
 this.map.clear();
 this.map.putall(map);
 }
 notifydatasetchanged();
 }
 public void notifyparent(list<string> parentlist) {
 if (parentlist != null) {
 this.parentlist.clear();
 this.parentlist.addall(parentlist);
 }
 notifydatasetchanged();
 }
 @override
 public int getchildrencount(int groupposition) {
 if (groupposition != -1) {
 string key = parentlist.get(groupposition);
 return map.get(key).size();
 } else {
 return 0;
 }
 }
 @override
 public object getchild(int groupposition, int childposition) {
 string key = parentlist.get(groupposition);// 根据组名位置的值作为map的key去获取value
 dtgzscustomer dtgzscustomer = map.get(key).get(childposition);
 return dtgzscustomer.sales_consultant_name;
 }
 @override
 public long getchildid(int groupposition, int childposition) {
 return childposition;
 }
 @override
 public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) {
 convertview = inflater.inflate(r.layout.child, null);
 textview text = (textview) convertview.findviewbyid(r.id.childto);
 imageview iv = (imageview) convertview.findviewbyid(r.id.iv);
 // 判断item的位置是否相同,如相同,则表示为选中状态,更改其背景颜色,如不相同,则设置背景色为白色
 if (group_groupid == groupposition && child_childid == childposition) {
 iv.setimageresource(r.drawable.login_check);
 } else {
 iv.setimageresource(r.drawable.login_uncheck);
 }
 string key = parentlist.get(groupposition);
 list<dtgzscustomer> list = map.get(key);
 dtgzscustomer childernitem = list.get(childposition);
 salestools.settextviewtext(text, childernitem.sales_consultant_name);
 return convertview;
 }
 @override
 public object getgroup(int groupposition) {
 return parentlist.get(groupposition);
 }
 @override
 public int getgroupcount() {
 return parentlist.size();
 }
 @override
 public long getgroupid(int groupposition) {
 return groupposition;
 }
 @override
 public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) {
 convertview = inflater.inflate(r.layout.group, null);
 imageview iv = (imageview) convertview.findviewbyid(r.id.groupicon);
 textview tv = (textview) convertview.findviewbyid(r.id.groupto);
 if (isexpanded) {
 iv.setimageresource(r.drawable.btn_arrow_up);
 } else {
 iv.setimageresource(r.drawable.btn_arrow_down);
 }
 salestools.settextviewtext(tv, parentlist.get(groupposition));
 return convertview;
 }
 @override
 public boolean hasstableids() {
 return true;
 }
 @override
 public boolean ischildselectable(int groupposition, int childposition) {
 return true;
 }
 @override
 public int getheaderstate(int groupposition, int childposition) {
 final int childcount = getchildrencount(groupposition);
 if (childposition == childcount - 1) {
 return pinned_header_pushed_up;
 } else if (childposition == -1 && !listview.isgroupexpanded(groupposition)) {
 return pinned_header_gone;
 } else {
 return pinned_header_visible;
 }
 }
 @override
 public void configureheader(view header, int groupposition, int childposition, int alpha) {
 string groupdata = this.parentlist.get(groupposition);
 ((textview) header.findviewbyid(r.id.groupto)).settext(groupdata);
 }
 private sparseintarray groupstatusmap = new sparseintarray();
 @override
 public void setgroupclickstatus(int groupposition, int status) {
 groupstatusmap.put(groupposition, status);
 }
 @override
 public int getgroupclickstatus(int groupposition) {
 if (groupstatusmap.keyat(groupposition) >= 0) {
 return groupstatusmap.get(groupposition);
 } else {
 return 0;
 }
 }
 public void notifydatasetchanged() {// 通知数据发生变化
 super.notifydatasetchanged();
 }
 }

子条目点击事件处理:

// 子条目的点击事件
 @override
 public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) {
 // toast.maketext(salenameactivity.this, "点击了" + groupposition +
 // childposition, toast.length_long).show();
 // 将被点击的一丶二级标签的位置记录下来
 string key = groupdata.get(groupposition);
 list<dtgzscustomer> list = map.get(key);
 dtgzscustomer dtgzscustomer = list.get(childposition);
 sales_consultant_name = dtgzscustomer.sales_consultant_name;
 sales_consultant_id = dtgzscustomer.sales_consultant_id;
 group_groupid = groupposition;
 child_childid = childposition;
 // 刷新界面
 adapter.notifydatasetchanged();
 return true;
 }

listview和edittext实现批量输入:

class setadapter extends baseadapter {
 private list<dtgzscustomer> goallist;
 private context context;
 public setadapter(context context, list<dtgzscustomer> goallist) {
 this.context = context;
 this.goallist = new arraylist<dtgzscustomer>();
 if (goallist != null) {
 this.goallist.addall(goallist);
 }
 }
 public void nodfiydata(list<dtgzscustomer> goallist) {
 if (goallist != null) {
 this.goallist.clear();
 this.goallist.addall(goallist);
 }
 notifydatasetchanged();
 }
 @override
 public int getcount() {
 return goallist.size();
 }
 @override
 public object getitem(int position) {
 return goallist.get(position);
 }
 @override
 public long getitemid(int position) {
 return position;
 }
 @override
 public view getview(final int position, view convertview, viewgroup parent) {
 viewholder holder;
 if (convertview == null) {
 holder = new viewholder();
 convertview = layoutinflater.from(context).inflate(r.layout.serise_data_view, parent, false);
 holder.et_order = (edittext) convertview.findviewbyid(r.id.et_order);
 holder.et_car = (edittext) convertview.findviewbyid(r.id.et_car);
 holder.sale_name = (textview) convertview.findviewbyid(r.id.sale_name);
 convertview.settag(holder);
 } else {
 holder = (viewholder) convertview.gettag();
 }
 if (position % 2 == 0) {
 convertview.setbackgroundcolor(color.parsecolor("#e4ebf1"));
 } else {
 convertview.setbackgroundcolor(color.parsecolor("#ced7de"));
 }
 final dtgzscustomer dtgzscustomer = goallist.get(position);
 removetextwatcher(holder.et_order);
 removetextwatcher(holder.et_car);
 string ordernum = dtgzscustomer.order_num;
 holder.et_order.settext(""+ordernum);
 string returnnum = dtgzscustomer.return_num;
 holder.et_car.settext(""+returnnum);
 holder.sale_name.settext(""+dtgzscustomer.sales_consultant_name);
 textwatcher ordertitle = new simpletextwatcher() {
 @override
 public void aftertextchanged(editable s) {
  int sum=0;
  if (textutils.isempty(s)) {
  dtgzscustomer.order_num="";
  } else {
  dtgzscustomer.order_num=string.valueof(s).replace("0", "");
  }
  string sales_consultant_id = dtgzscustomer.sales_consultant_id;
  if (!ordermap.containskey(sales_consultant_id)) {
  string order_num = dtgzscustomer.order_num.replace("0", "");
  ordermap.put(sales_consultant_id, order_num);
  }else{
  ordermap.remove(sales_consultant_id);  
  string order_num = dtgzscustomer.order_num.replace("0", "");
  ordermap.put(sales_consultant_id, order_num);
  }
  iterator<map.entry<string, string>> it = ordermap.entryset().iterator();
  while(it.hasnext()){
  map.entry<string, string> entry = it.next();
  string value = entry.getvalue();
  if (value==null||value.length()==0) {
  value="0";
  }
  sum=sum+integer.parseint(value);
  }
  tv_order.settext("订车"+sum+"台");
 }
 };
 holder.et_order.addtextchangedlistener(ordertitle);
 holder.et_order.settag(ordertitle);
 textwatcher carcontent = new simpletextwatcher() {
 @override
 public void aftertextchanged(editable s) {
  int sum=0;
  if (textutils.isempty(s)) {
  dtgzscustomer.return_num="";
  } else {
  dtgzscustomer.return_num=string.valueof(s).replace("0", "");
  }
  string sales_consultant_id = dtgzscustomer.sales_consultant_id;
  if (!carmap.containskey(sales_consultant_id)) {
  string return_num = dtgzscustomer.return_num.replace("0", "");
  carmap.put(sales_consultant_id, return_num);
  }else{
  carmap.remove(sales_consultant_id);  
  string return_num = dtgzscustomer.return_num.replace("0", "");
  carmap.put(sales_consultant_id, return_num);
  }
  iterator<map.entry<string, string>> it = carmap.entryset().iterator();
  while(it.hasnext()){
  map.entry<string, string> entry = it.next();
  string value = entry.getvalue();
  if (value==null||value.length()==0) {
  value="0";
  }
  sum=sum+integer.parseint(value);
  }
  tv_return.settext("交车"+sum+"台");
 }
 };
 holder.et_car.addtextchangedlistener(carcontent);
 holder.et_car.settag(carcontent);
 return convertview;
 }
 /**
 * 去除textwatcher
 *
 * @param edittext
 */
 private void removetextwatcher(edittext edittext) {
 if (edittext.gettag() instanceof textwatcher) {
 edittext.removetextchangedlistener((textwatcher) edittext.gettag());
 }
 }
 }
 class viewholder {
 edittext et_order, et_car;
 textview sale_name;
 }

以下是效果显示:

Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码 

源码地址:http://xiazai.jb51.net/201702/yuanma/listdemo

以上所述是小编给大家介绍的android listview expandablelistview实现多选,单选,全选,edittext实现批量输入的实例代码,希望对大家有所帮助

上一篇:

下一篇: