zoukankan      html  css  js  c++  java
  • 利用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>  
  • 相关阅读:
    拜访优达学城上海总部
    ipad pro 文章
    探寻阿里云服务器迈入2.0时代的技术要点
    体验阿里云SSD云盘+ECS(I/O优化)的性能
    在iOS中使用OpenSSL的Public Key 进行加密
    使用FIR.im发布自己的移动端APP
    使用AFNetWorking 实现以Basic Authentication方式获取access-token
    使用AFNetWorking读取JSON出现NSCocoaErrorDomain Code=3840的解决方法
    #听云博客大赛#如何在自己的App嵌入听云产品监控App性能
    给自己的XTC820摆拍一下。
  • 原文地址:https://www.cnblogs.com/guop/p/5101296.html
Copyright © 2011-2022 走看看