Android開發之ListView條目批量選擇刪除

來源:互聯網
上載者:User

Android開發之ListView條目批量選擇刪除

ListView實現的列表,如果是可編輯,可刪除的,一般都要提供大量刪除功能,否則的話,一項一項的刪除體驗很不好,也給使用者帶來了很大的麻煩。

實現

具體實現代碼

select.xml<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+Ctb3sry+1s7EvP6w/Lqs0ru49kxpc3RWaWV3u7nT0NK7uPbS/rLYtcSyvL7Wo6yw/LqswcvBvbj2QnV0dG9u0ru49lRleHRWaWV3LMSsyM+yvL7Wzqpnb25lo6y1sbzgzP21vbOksLTP7NOmysK8/sqxuvLP1Mq+oaM8L3A+Cgo8aW1nIGlkPQ=="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

包含一個TextView 一個CheckBox

 1  2  6     7     19     20     27 

通過自訂Adapter綁定ListView資料來源,實現長按監聽,在長按監聽時候,切記將監聽事件返回ture。

  1 /**  2      * @author ieasy360_1  3      * 自訂Adapter  4      */  5     class Adapter extends BaseAdapter{  6         private Context context;  7         private LayoutInflater inflater=null;  8         private HashMap mView ;  9         public  HashMap visiblecheck ;//用來記錄是否顯示checkBox 10         public  HashMap ischeck; 11         private TextView txtcount; 12         public Adapter(Context context,TextView txtcount) 13         { 14             this.context = context; 15             this.txtcount = 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

全部實現代碼

  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 listview; 30     private Context  context; 31     private List array = new ArrayList(); 32     private List selectid = new ArrayList(); 33     private boolean isMulChoice = false; //是否多選 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 = (TextView)findViewById(R.id.txtcount); 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("小明"+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 ;//用來記錄是否顯示checkBox113         public  HashMap ischeck;114         private TextView txtcount;115         public Adapter(Context context,TextView txtcount)116         {117             this.context = context;118             this.txtcount = 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.