欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android使用ListView批量删除item的方法

程序员文章站 2024-03-03 22:48:52
本文实例讲述了android使用listview批量删除item的方法。分享给大家供大家参考,具体如下: 利用checkbox选中一个或多个item,最后批量删除它们。...

本文实例讲述了android使用listview批量删除item的方法。分享给大家供大家参考,具体如下:

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

程序运行效果图如下:

Android使用ListView批量删除item的方法 Android使用ListView批量删除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>

更多关于android相关内容感兴趣的读者可查看本站专题:《android编程之activity操作技巧总结》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android视图view技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。