本文实例讲述了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程序设计有所帮助。
新闻热点
疑难解答