Batch Delete ListView entries for Android Development
If the list implemented by ListView is editable and can be deleted, batch deletion is generally required. Otherwise, the deletion experience of one item is poor, it also brings a lot of trouble to users.
Implementation
Specific implementation code
Select. xml <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Ctb3sry + 1s7EvP6w/release + export/sqxuvLP1Mq + oaM8L3A + release = "code_img_opened_cde9cf76-2928-403a-9773-41eea77ffed9" class = "code_img_opened" src = "http://www.bkjia.com/uploads/allimg/140920/04130G624-4.gif" alt = "\">
1
2
6 7
14 15
16 17
26
37
47 48
59 60 61
62
Item. xml
Contains a TextView and a CheckBox
1
2
6 7
19 20
27
You can bind a custom Adapter to the ListView data source to enable a long-press listener. Remember to return a true event when listening.
1/** 2 * @ author ieasy360_1 3 * Custom Adapter 4 */5 class Adapter extends BaseAdapter {6 private Context context Context; 7 private LayoutInflater inflater = null; 8 private HashMap
MView; 9 public HashMap
Visiblecheck; // used to record whether checkBox 10 public HashMap is displayed
Ischeck; 11 private TextView txtcount; 12 public Adapter (Context context, TextView txtcount) 13 {14 this. context = context; 15 this.txt count = txtcount; 16 inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); 17 mView = new HashMap
(); 18 visiblecheck = new HashMap
(); 19 ischeck = new HashMap
(); 20 if (isMulChoice) {21 for (int I = 0; I
All implementation code
1 package com. example. test; 2 import java. util. arrayList; 3 import java. util. hashMap; 4 import java. util. list; 5 import android. app. activity; 6 import android. content. context; 7 import android. OS. bundle; 8 import android. view. contextMenu; 9 import android. view. contextMenu. contextMenuInfo; 10 import android. view. layoutInflater; 11 import android. view. view; 12 import android. view. view. onClickListener; 13 import android. view. view. onLongClickListener; 14 import android. view. viewGroup; 15 import android. widget. baseAdapter; 16 import android. widget. button; 17 import android. widget. checkBox; 18 import android. widget. listView; 19 import android. widget. relativeLayout; 20 import android. widget. textView; 21 import android. widget. toast; 22 23/** 24 * @ author ieasy360_1 25*26 */27 public class MulSelect extends Activity implements OnClickListener {28 29 private ListView; 30 private Context context Context; 31 private List
Array = new ArrayList
(); 32 private List
Selectid = new ArrayList
(); 33 private boolean isMulChoice = false; // whether to select 34 private Adapter adapter; 35 private RelativeLayout layout; 36 private Button cancle, delete; 37 private TextView txtcount; 38 39 @ Override 40 protected void onCreate (Bundle savedInstanceState) {41 // TODO Auto-generated method stub 42 super. onCreate (savedInstanceState); 43 setContentView (R. layout. select); 44 context = this; 45 listview = (ListView) findViewById (R. id. list); 46 layout = (RelativeLayout) findViewById (R. id. relative); 47 txtcount = (textview1_findviewbyid(r.id.txt count); 48 cancle = (Button) findViewById (R. id. cancle); 49 delete = (Button) findViewById (R. id. delete); 50 cancle. setOnClickListener (this); 51 delete. setOnClickListener (this); 52 init (); 53 adapter = new Adapter (context, txtcount); 54 listview. setAdapter (adapter); 55 56} 57 58 void init () 59 {60 for (int I = 0; I <20; I ++) 61 {62 array. add ("James" + I); 63} 64} 65 66 public void onClick (View v) {67 // TODO Auto-generated method stub 68 switch (v. getId () {69 case R. id. cancle: 70 isMulChoice = false; 71 selectid. clear (); 72 adapter = new Adapter (context, txtcount); 73 listview. setAdapter (adapter); 74 layout. setVisibility (View. INVISIBLE); 75 break; 76 case R. id. delete: 77 isMulChoice = false; 78 for (int I = 0; I
Visiblecheck; // used to record whether checkBox113 public HashMap is displayed
Ischeck; 114 private TextView txtcount; 115 public Adapter (Context context, TextView txtcount) 116 {117 this. context = context; 118 this.txt count = txtcount; 119 inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); 120 mView = new HashMap
(); 121 visiblecheck = new HashMap
(); 122 ischeck = new HashMap
(); 123 if (isMulChoice) {124 for (int I = 0; I