Android's listview Multiple-selection deletion operation implementation code _android

Source: Internet
Author: User
Tags gettext static class stub cbtn

Recently tried to do a ListView multiple selection demo, the Internet to see other people's example feeling is not very difficult, do-it-yourself, the various details of the problem, not so simple ah. Now that you've done it, simply write a note down.
Practicing demo, name notes Chaos, don't mind oh.

Main interface Layout Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" match_parent "android:id=" @ +id/rootview "android:paddingbottom=" @dimen/activity_vertical_margin "android:paddingleft=" @dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.example.listchecked.MainActivity "> <listview android:id=" @+id/listview1 

 "Android:layout_width=" match_parent "android:layout_height=" wrap_content "android:layout_marginbottom=" 50DP "> </ListView> <linearlayout android:id= "@+id/button_group" android:layout_width= "Match_parent" android:l
  ayout_height= "Wrap_content" android:layout_alignleft= "@+id/listview1" android:layout_alignparentbottom= "true" android:orientation= "vertical" > <button android:id= "@+id/del" AndrOid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "delete"/> </LinearLayout>

 </RelativeLayout>

Layout of list item

<?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=" match_parent "android:orientation=" Horizontal "Android:descendantfocusability=" Blocksdescendants > <!--attention to this property, is critical, Without adding the list will not respond to events in Onitemclicklistener--> <relativelayout android:layout_width= "Match_parent" Android:layout_ height= "Match_parent" android:layout_weight= "1" > <checkbox android:id= "@+id/checkbox1" android:layout _width= "Wrap_content" android:layout_height= "Wrap_content" android:clickable= "false" android:focusable= "false"/&

  Gt <imageview android:id= "@+id/imageview1" android:layout_width= match_parent "android:layout_height=" Match_paren T "android:scaletype=" Fitcenter "android:src=" @drawable/ic_launcher "/> </RelativeLayout> <linearlay Out android:layout_width= "match_parent" android:layout_height= "Match_parenT "android:layout_weight=" 1 "android:gravity=" center "android:orientation=" vertical "> <textview Android
   : id= "@+id/title" android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "title" Android:textsize= "20sp"/> <textview android:id= "@+id/teacher" android:layout_width= "Match_parent" Andr oid:layout_height= "Wrap_content" android:text= "Teacher"/> <textview android:id= "@+id/time" Android:layo Ut_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Time"/> <textview = "@+id/peoplenum" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "PeopleNu"

 M "/> </LinearLayout> </LinearLayout>

The layout of this item is in the details of the pit once, the item added Button,checkbox,imagebutton, etc., will cause only the response to these control events, the item overall event will not respond, just started without that attribute, check the checkbox, Point deletion did not respond. This problem to find also a bit difficult, before see the great God has recommended the analysis of the layout of the tool hierarchyviewer, this tool, personal feeling and front-end development in the F12 similar ah, very convenient, you can see the situation of each layout. is also curious, point open a look, the entire item layout, from the parent layout, to the child layout, as long as the checkbox can accept click, all the other is false, this is the problem, and then Baidu a bit, as on the conclusion.

The solution is to add android:descendantfocusability= "blocksdescendants" to the list's item layout, and when added, the checkbox can still be clicked individually, It's strange to not respond to the list's click-selection event, which is not in the case of others. Finally, you can only set the checkbox cannot be clicked Android:clickable= "false" layout problem solved, the following is the Java class source code:
First is the Bean class, Itembean.java

Package com.example.listchecked;
 public class Itembean {private int imgres;
 Private String Title,teacher,time;
 private int peoplenum,id;
 Private Boolean ischecked;

 Private Boolean isshow;
 public Boolean isshow () {return isshow;
 } public void Setshow (Boolean isshow) {this.isshow = Isshow;
 public int getId () {return id;
 The public void setId (int id) {this.id = ID;
 public Boolean ischecked () {return ischecked;
 } public void Setchecked (Boolean ischecked) {this.ischecked = ischecked;
 public int getimgres () {return imgres;
 public void setimgres (int img) {this.imgres = img;
 Public String GetTitle () {return title;
 public void Settitle (String title) {this.title = title;
 Public String Getteacher () {return teacher;
 } public void Setteacher (String teacher) {this.teacher = teacher;
 Public String GetTime () {return time;
 public void SetTime (String time) {this.time = time; public int Getpeoplenum () {REturn Peoplenum;
 The public void setpeoplenum (int peoplenum) {this.peoplenum = Peoplenum;
 }

}

Custom Adapter,mylistadapter.java

Package com.example.listchecked;

Import java.util.List;
Import Android.content.Context;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.CheckBox;
Import Android.widget.CompoundButton;
Import Android.widget.CompoundButton.OnCheckedChangeListener;
Import Android.widget.ImageView;

Import Android.widget.TextView;
 public class Mylistadapter extends Baseadapter {private Layoutinflater inflater;
 Private list<itembean> items;
 Private Itembean item;

 Private Onshowitemclicklistener Onshowitemclicklistener;
 Public Mylistadapter (list<itembean> list,context context) {items=list;
 Inflater=layoutinflater.from (context);
 @Override public int GetCount () {//TODO automatically generated method stub return Items.size ();
 @Override public Object getitem (int position) {//TODO automatically generated method stub return Items.get (position); @Override public long getitemid (int position) {//TODO automatically generated method stub retUrn Items.get (position). GetId (); @Override public View getview (int position, View Convertview, ViewGroup parent) {//TODO automatically generated method stub Viewholder
  er;//uses Viewholder, the great God says can promote performance if (Convertview==null) {holder=new viewholder ();
  Convertview=inflater.inflate (R.layout.item_view, NULL);
  Holder.img= (ImageView) Convertview.findviewbyid (R.ID.IMAGEVIEW1);
  holder.cb= (CheckBox) Convertview.findviewbyid (r.id.checkbox1);
  Holder.title= (TextView) Convertview.findviewbyid (r.id.title);
  Holder.teacher= (TextView) Convertview.findviewbyid (R.id.teacher);
  Holder.time= (TextView) Convertview.findviewbyid (r.id.time);
  Holder.poeple= (TextView) Convertview.findviewbyid (r.id.peoplenum);
 Convertview.settag (holder);
 }else {holder= (Viewholder) Convertview.gettag ();
 } item=items.get (position);
 if (Item.isshow ()) {holder.cb.setVisibility (view.visible);
 else {holder.cb.setVisibility (view.gone);
 } holder.img.setImageResource (Item.getimgres ()); Holder.title.setText (item.gettitlE ());
 Holder.teacher.setText ("Speaker:" +item.getteacher ());
 Holder.time.setText ("Lesson:" +item.gettime () + "speak");

 Holder.poeple.setText ("Number of Learners:" +item.getpeoplenum ()); Holder.cb.setOnCheckedChangeListener (New Oncheckedchangelistener () {@Override public void oncheckedchanged (compound
  Button Buttonview, Boolean ischecked) {if (ischecked) {item.setchecked (true);
  else {item.setchecked (false);
  }//callback method, add item to the selected Onshowitemclicklistener.onshowitemclick (item);
 }
 });
 After listening, set the selection status holder.cb.setChecked (item.ischecked ());
 return convertview;
 Static class Viewholder {ImageView img;
 CheckBox CB;

 TextView title,teacher,time,poeple;
 Public interface Onshowitemclicklistener {public void Onshowitemclick (Itembean bean);
 } public void Setonshowitemclicklistener (Onshowitemclicklistener onshowitemclicklistener) {
This.onshowitemclicklistener = Onshowitemclicklistener;

 }
}

And finally, Mainactivity.java.

Package com.example.listchecked;
Import java.util.ArrayList;

Import java.util.List;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemClickListener;
Import Android.widget.Button;
Import Android.widget.LinearLayout;
Import Android.widget.ListView;
Import Android.widget.RelativeLayout;

Import Android.widget.Toast;

Import Com.example.listchecked.MyListAdapter.OnShowItemClickListener;
 public class Mainactivity extends activity implements Onshowitemclicklistener {private ListView ListView;
 Private list<itembean> datalist,selectedlist;
 Private Mylistadapter Myadapter;
 Private Relativelayout Rootview;
 Private LinearLayout Menuview;
 Private LinearLayout OpenView;

 private static Boolean isshow; @Override protected void OnCreate (BundleSavedinstancestate) {super.oncreate (savedinstancestate);
 Isshow=false;
 Setcontentview (R.layout.activity_main);
 Button delbtn= (button) Findviewbyid (R.id.del);
 rootview= (relativelayout) Findviewbyid (R.id.rootview);
 menuview= (LinearLayout) Findviewbyid (R.id.button_group);
 listview= (ListView) Findviewbyid (R.ID.LISTVIEW1);
 Datalist=new arraylist<itembean> ();
 Selectedlist=new arraylist<itembean> ();
  for (int i=0;i<10;i++) {Itembean item=new itembean ();
  Item.setid (i);
  Item.setimgres (R.drawable.ic_launcher);
  Item.settitle ("First" +item.getid () + "a");
  Item.setteacher ("Miss Yang");
  Item.settime ("34");
  Item.setpeoplenum (i+1*100);
  Item.setchecked (FALSE);
  Item.setshow (isshow);
 Datalist.add (item);
 } myadapter=new Mylistadapter (dataList, this);
 Myadapter.setonshowitemclicklistener (Mainactivity.this);

 Listview.setadapter (Myadapter); Delbtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO automatically generated method stubs SHOWM
  ENU ();Isshow=true;
  Selectedlist.clear ();
  for (Itembean item:datalist) {item.setshow (isshow);

  } myadapter.notifydatasetchanged ();
 }
 }); Listview.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapterview<?> pa Rent, view view, int position, long ID) {//TODO automatically generated method stub if (isshow) {Itembean item = datalist.get (position)
   ;
   Boolean ischecked = item.ischecked ();
   if (ischecked) {item.setchecked (false);
   else {item.setchecked (true);
   } myadapter.notifydatasetchanged ();
  LOG.D ("Select", Selectedlist.size () + "");

 }
  }
 }); //Show menu selected for deletion, private void ShowMenu () {relativelayout.layoutparams lp=new relativelayout.layoutparams (relativelayout .
 Layoutparams.match_parent, RelativeLayout.LayoutParams.WRAP_CONTENT);
 Lp.addrule (Relativelayout.align_parent_bottom);
 Layoutinflater Inflater = (layoutinflater) this.getsystemservice (Context.layout_inflater_service); openview= (LinearLayout) inflater.Inflate (r.layout.delmenu_layout, NULL);
 Rootview.removeview (Menuview);
 Rootview.addview (OPENVIEW,LP);
 Final button sbtn= (button) Openview.findviewbyid (R.id.selectall);
 Button dbtn= (button) Openview.findviewbyid (R.id.del_button);
 Button cbtn= (button) Openview.findviewbyid (R.id.cancel_button); Sbtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO automatically generated method stubs if ("Select All ". Equals (Sbtn.gettext (). toString ())) {for (Itembean bean:datalist) {if (!bean.ischecked ()) {Bean.setcheck
     Ed (true);
     if (!selectedlist.contains (Bean)) {selectedlist.add (bean);
   }} myadapter.notifydatasetchanged ();
  Sbtn.settext ("Anti-election");
    else if ("Sbtn.gettext". Equals (). ToString ()) {for (Itembean bean:datalist) {bean.setchecked (false);
    if (!selectedlist.contains (Bean)) {selectedlist.remove (bean);
   } myadapter.notifydatasetchanged ();
  Sbtn.settext ("Select All");

 }
  }
 }); Dbtn.setonclicklisteNER (new Onclicklistener () {@Override public void OnClick (View v) {//TODO automatically generated method stubs if (selectedlist!=null)
   ;& selectedlist.size () >0) {Datalist.removeall (selectedlist);
   Myadapter.notifydatasetchanged ();
  Selectedlist.clear ();
  else {toast.maketext (mainactivity.this, "Please select Entry", Toast.length_short). Show ();
 }
  }
 }); Cbtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO automatically generated method stubs if (IsS
   how) {selectedlist.clear ();
    for (Itembean bean:datalist) {bean.setchecked (false);
   Bean.setshow (FALSE);
   } myadapter.notifydatasetchanged ();
   Isshow = false;
  Restoreview ();

 }
  }
 }); @Override public void Onshowitemclick (Itembean bean) {//TODO automatically generated method stubs if (bean.ischecked () &&!selectedli
 St.contains (Bean)) {selectedlist.add (bean);
 else if (!bean.ischecked () && selectedlist.contains (Bean)) {selectedlist.remove (bean);
}} private void Restoreview () { Rootview.addview (Menuview);
  if (openview!=null) {Rootview.removeview (OpenView);
 Openview=null;

 }

 }
}

Finally there is the layout of the small menu, or put it on

<?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=" match_parent "
 android:o" rientation= "Horizontal" >

 <button
  android:id= "@+id/selectall" android:layout_width= "WRAP_"
  Content "
  android:layout_height=" wrap_content "
  android:layout_weight=" 1 "
  android:text=" Select All "/>

 <button
  android:id= "@+id/del_button"
  android:layout_width= "Wrap_content"
  android: layout_height= "Wrap_content"
  android:layout_weight= "1"
  android:text= "delete"/>

 <button
  Android:id= "@+id/cancel_button"
  android:layout_width= "wrap_content"
  android:layout_height= "WRAP_" Content "
  android:layout_weight=" 1 "
  android:text=" Cancel "/>

</LinearLayout>

All the code is put on, if you do not want to copy paste, GitHub address: https://github.com/2767321434/ListChecked

The above is the entire contents of this article, I hope to learn more about Android software programming help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.