Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之ListView條目批量選擇刪除

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

編輯:關於Android編程

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

實現效果圖

\\\\

具體實現代碼

select.xml<喎?/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="https://www.android5.online/Android/UploadFiles_5356/201702/2017022316333911.gif" alt="\"> 復制代碼

 1 
 2 
 6     
 7     
14 
15         
16         
17     
26         
復制代碼

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 ;//用來記錄是否顯示checkBox
113         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
復制代碼


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved