• 利用ListView批量删除item


    利用CheckBox选中一个或多个item,最后批量删除它们。 

    程序运行效果图如下:

    package com.test.adapter;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.ListActivity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.CheckBox;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.test.adapter.ArrayAdapterActivity.ListVIewAdapter.ViewHolder;
    
    
    /**
     * ListView 批量删除Item
     * @author manymore13
     *
     */
    public class ArrayAdapterActivity extends ListActivity {  
         
        private ListVIewAdapter ladapter;
        private List<String> strList = new ArrayList<String>();
        private List<Boolean> boolList = new ArrayList<Boolean>();
          
        boolean visflag = false;  
        ListView lv;  
        static String str[] ={"1苹果","2香蕉","3桔子","4猕猴桃","5李子","6甘蔗","7荔枝","8桃子","9香瓜","10葡萄","11哈密瓜","12桂圆","13龙眼","14","15","16","17","18","19","20"};  
        CheckBox cb;  
        {  
            for(int i=0;i<str.length;i++)  
            {  
                strList.add(str[i]);
                boolList.add(false);
            }  
        }  
      
          
        @Override  
        protected void onCreate(Bundle savedInstanceState)  
        {  
              
            super.onCreate(savedInstanceState);  
                  
            ladapter = new ListVIewAdapter(this);  
              
            lv = this.getListView();  
            lv.setAdapter(ladapter);  
            
            lv.setScrollBarStyle(1);  
      
            lv.setOnItemClickListener(new OnItemClickListener()  
            {  
      
                @Override  
                public void onItemClick(AdapterView<?> parent,  
                        View view, int position, long id)  
                {  
                    if(visflag)
                    {
                        ViewHolder viewHolder = (ViewHolder) view.getTag();
                        viewHolder.cb.toggle();
                         if(viewHolder.cb.isChecked())
                        {
    
                            boolList.set(position, true);
                        }else{
                            boolList.set(position, false);
                        }
                     }
                }  
            });  
              
        }  
          
        class ListVIewAdapter extends BaseAdapter  
        {  
              
            Context c;  
            LayoutInflater mInflater ;  
            ListVIewAdapter(Context context)  
            {  
                c = context;  
                mInflater = getLayoutInflater();  
            }  
            @Override  
            public int getCount()  
            {  
                return strList.size();  
            }  
      
            @Override  
            public Object getItem(int position)  
            {  
                return strList.get(position);  
            }  
      
            @Override  
            public long getItemId(int position)  
            {  
                return position;  
            }  
              
      
            @Override  
            public View getView(final int position, View convertView,  
                    ViewGroup parent)  
            {  
                ViewHolder holder = null ;  
                if(convertView == null)  
                {  
                    
                    holder = new ViewHolder();
                    convertView  = mInflater.inflate(R.layout.list_item, null);
                    holder.tv = (TextView)convertView.findViewById(R.id.textView);  
                    holder.cb = (CheckBox)convertView.findViewById(R.id.checkBox);
                    convertView.setTag(holder);
                    
                }else{
                    holder = (ViewHolder) convertView.getTag();
                } 
                holder.tv.setText(strList.get(position));  
                holder.cb.setChecked(boolList.get(position));  
                  
                if(visflag)  
                {  
                    holder.cb.setVisibility(View.VISIBLE);  
                }  
                else  
                {  
                    holder.cb.setVisibility(View.INVISIBLE);  
                }  
              
                return convertView;  
            }  
            class ViewHolder  
            {  
                TextView tv;  
                CheckBox cb;  
            }  
              
        }  
      
        @Override  
        public boolean onCreateOptionsMenu(Menu menu)  
        {  
              
            menu.add(0, 0, 0, "批量处理");  
            menu.add(0, 1, 0, "确定删除");  
            return super.onCreateOptionsMenu(menu);  
        }  
      
        @Override  
        public boolean onOptionsItemSelected(MenuItem item)  
        {  
            switch(item.getItemId())  
            {  
                case 0:  // 批量处理  
                    {  
                        if(visflag)  
                        {  
                            visflag = false;  
                            for(int i=0; i<boolList.size();i++)
                            {
                                boolList.set(i, false);
                            }
                        }  
                        else  
                        {  
                            visflag = true;  
                        }  
                        this.ladapter.notifyDataSetInvalidated();  
                        break;  
                    }  
                case 1: //确定删除  
                    {  
                        if(boolList.size()>0)  
                        {  
                            if(visflag)
                            {
                                for(int location=0; location<boolList.size(); )
                                {
                                    if(boolList.get(location))
                                    {
                                        boolList.remove(location);
                                        strList.remove(location);
                                        continue;
                                    }
                                    location++;
                                }
                            }
                              
                        }
                        this.ladapter.notifyDataSetChanged();
                        break;  
                    }  
            }  
            return super.onOptionsItemSelected(item);  
        }  
          
          
    }  

    list_item.xml 

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        >  
        <RelativeLayout   
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content">  
            <TextView    
                android:id="@+id/textView"  
                android:layout_width="wrap_content"   
                android:layout_height="wrap_content"  
                android:text="@string/hello"  
                android:layout_alignParentLeft="true"  
                android:layout_centerVertical="true"  
                  
                />  
            <CheckBox   
                android:id="@+id/checkBox"  
                android:layout_width="wrap_content"   
                android:layout_height="wrap_content"  
                android:layout_alignParentRight="true"  
                android:visibility="gone"  
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:clickable="false"  
               />  
        </RelativeLayout>  
    </LinearLayout>  
  • 相关阅读:
    IO学习BufferedWriter 规格严格
    Finalization 规格严格
    linux下查看主板内存槽与内存信息 规格严格
    调试JavaScript/VB Script脚本程序(Wscript篇) 规格严格
    ORA01688:unable to extend table name。name partition NAME by NUM in tablespace NAME 规格严格
    Hidden Java 7 Features – SecondaryLoop 规格严格
    Web应用服务器监控 规格严格
    NetBeans 时事通讯(刊号 # 12 Jun 16, 2008)
    NetBeans 时事通讯(刊号 # 12 Jun 16, 2008)
    Win32 DLL的一个调试心得
  • 原文地址:https://www.cnblogs.com/guop/p/5101296.html
Copyright © 2020-2023  润新知