The previous section, has completed the call Butler interface design, then the following to achieve specific functions, how to add the black and white list to display it? Here to use the ListView, then, if you need to remove the black and white list, is a long press the pop-up menu to delete, or the black and white list is emptied, which are not in line with the needs of users, often, are deleted multiple, there is a problem, how to delete the specified multiple item in the ListView?? Maybe you think of it, you need to use a checkbox.
First look at the picture:
As you can see, when in delete mode, the bottom button becomes deleted and returned, the middle also shows how many items are currently selected, and a checkbox is displayed to the right of each item in the ListView for multiple selections.
In this section, only how to display and add listeners to the ListView, the next section implements the addition and deletion of black and white lists.
Black and white list layout file on the previous section has been given, since the use of the ListView, must have adapter, here is still using Baseadapter, the main code is as follows:
Class Adapter extends baseadapter{private context context; Private Layoutinflater Inflater=null; Private Hashmap<integer, view> MView; Public Hashmap<integer, integer> visiblecheck;//used to record whether the checkbox is displayed public Hashmap<integer, Boolean> is Check Private TextView Txtcount; Public Adapter (Context Context,textview txtcount) {this.context = context; This.txtcount = Txtcount; Inflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service); MView = new Hashmap<integer, view> (); Visiblecheck = new Hashmap<integer, integer> (); Ischeck = new Hashmap<integer, boolean> (); if (Ismulchoice) {for (int i=0;i<array.size (); i++) {Ischeck.put (I, false); Visiblecheck.put (i, checkbox.visible); }}else{for (int i=0;i< Array.size (); i++) {Ischeck.put (I, false); Visiblecheck.put (i, checkbox.invisible); }}} public int GetCount () {//TODO auto-generated method stub return arr Ay.size (); } public Object GetItem (int position) {//TODO auto-generated Method stub return Array.get (P Osition); } public long Getitemid (int position) {//TODO auto-generated method stub return 0; Public View GetView (final int position, View Convertview, ViewGroup parent) {//TODO Auto-gene Rated method stub View view = Mview.get (position); if (view==null) {view = Inflater.inflate (R.layout.list_item, NULL); TextView id = (TextView) View.findviewbyid (r.id.tv_id); TextView name = (TextView) View.findviewbyid (r.id.tv_name); TextView number = (TextView) View.findviewbyid (R.id.tv_number); Final CheckBox CEB = (CheckBox) View.findviewbyid (r.id.cb_choose); Person P = array.get (position); Id.settext (p.id+ ""); Name.settext (P.name); Number.settext (P.number); Ceb.setchecked (Ischeck.get (position)); Ceb.setvisibility (Visiblecheck.get (position)); View.setonlongclicklistener (New Onlongclick ()); View.setonclicklistener (New Onclicklistener () {public void OnClick (View v) { TODO auto-generated Method stub if (ismulchoice) {if (Ceb.ischecked ()) {ceb.setchecked (false); Selectid.remove (Array.get (position)); }else{ Ceb.setchecked (TRUE); Selectid.add (Array.get (position)); } txtcount.settext ("Total selected" +selectid.size () + "item"); }else {//toast.maketext (context, "clicked" +array.get (position), Toast.length_long). Show (); } } }); Mview.put (position, view); } return view; } class Onlongclick implements onlongclicklistener{public boolean Onlongclick (View v) { TODO auto-generated Method Stub ismulchoice = true; Selectid.clear (); Add_layout.setvisibility (View.gone); Delete_layout.setvisibility (view.visible); for (int i=0;i<array.size (); i++) {Adapter.visiblecheck.put (I, checkbox.visible); } adapter = new adapter (context,txtcount); Lv_show.setadapter (adapter); return true; } } }
The Onlongclick is the ability to add a long-press listener to the ListView item, a popup checkbox, and a bottom delete button. In this case, the control is hidden and displayed, and the rest is the declaration and definition of the control. and binds the listener to the ListView.
Private list<person> array = new arraylist<person> (); Private list<person> Selectid = new arraylist<person> ();p rivate ListView lv_show;private CheckBox CB; Private TextView tv_count;private relativelayout add_layout;private relativelayout delete_layout;private Adapter Adapter
Where person is a custom class:
Person.java
public class Person {String name; String number; int id;public person (int id,string name,string number) {this.id = Id;this.name = Name;this.number = number;}}
Tv_count = (TextView) Findviewbyid (r.id.tv_select); Add_layout = (relativelayout) Findviewbyid (r.id.add_layout); Delete_layout = (relativelayout) Findviewbyid (r.id.delete_layout); Lv_show = (ListView) Findviewbyid (r.id.lv_show);
Here is just the main code, the complete code I will pack and upload.
Android Project Call Butler (2)-----The use of Listview+checkbox