亲宝软件园·资讯

展开

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

manymore13 人气:0
想了解Android使用ListView批量删除item的方法的相关内容吗,manymore13在本文为您仔细讲解Android使用ListView批量删除item的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ListView,批量删除,item,下面大家一起来学习吧。

本文实例讲述了Android使用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>

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

加载全部内容

相关教程
猜你喜欢
用户评论