Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android使用ListView批量刪除item的方法

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

編輯:關於Android編程

本文實例講述了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相關內容感興趣的讀者可查看本站專題:《Android編程之activity操作技巧總結》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數據庫技巧總結》、《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》、《Android視圖View技巧總結》及《Android控件用法總結》

希望本文所述對大家Android程序設計有所幫助。

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