ListView条目的多条删除

本文介绍了一种在Android应用中实现列表项多选及批量删除功能的方法。通过自定义Adapter和Bean类,实现了列表项的选择状态管理和UI更新。同时,提供了全选、取消选择、删除已选项等功能。

近期公司项目需求 需要一个实现多选,全选的删除操作,本人就试着写了一个demo 然后就发到上面  不是很好 大家可以借鉴下

这是主页面

import java.util.ArrayList;
import java.util.List;

import com.example.practisemoredel.ListViewAdapter.OnShowChangedListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnShowChangedListener,
OnClickListener {

	private ListView listview;
	private Button del,
	allchoose,
	del_choose,
	cancle;
	private List < Bean > list = new ArrayList < Bean > ();
	private List < Bean > mList = new ArrayList < Bean > ();
	private Bean bean;
	private ListViewAdapter adapter;
	private boolean isShow = false;

	@Override protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
		initData();
	}

	private void initData() {

		for (int i = 0; i < 10; i++) {
			bean = new Bean();
			bean.setChecked(false);
			bean.setShow(isShow);
			bean.setName("仓井空");
			list.add(bean);
		}
		adapter = new ListViewAdapter(list, getApplicationContext());
		adapter.setOnShowChangedListener(this);
		listview.setAdapter(adapter);

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override public void onItemClick(AdapterView < ?>arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				isShow = true;
				bean = list.get(arg2);
				if (bean.isChecked()) {
					bean.setChecked(false);
				} else {
					bean.setChecked(true);
				}
				adapter.notifyDataSetChanged();
			}
		});
	}

	private void initView() {
		listview = (ListView) findViewById(R.id.lv);
		del = (Button) findViewById(R.id.del);
		allchoose = (Button) findViewById(R.id.allchoose);
		del_choose = (Button) findViewById(R.id.del_choose);
		cancle = (Button) findViewById(R.id.cancle);

		allchoose.setOnClickListener(this);
		del_choose.setOnClickListener(this);
		cancle.setOnClickListener(this);
		del.setOnClickListener(this);

	}

	@Override public void isShowItemClickListener(Bean bean) {
		// 如果是选中状态  且集合中不包含这个对象就把这个对象添加到集合中
		if (bean.isChecked() && !mList.contains(bean)) {
			mList.add(bean);
		} else if (!bean.isChecked() && mList.contains(bean)) {
			mList.remove(bean);
		}
	}

	@Override public void onClick(View v) {
		// TODO Auto-generated method stub
		isShow = true; //点击删除后改变选中框展示状态
		switch (v.getId()) {
		case R.id.del:
			del.setVisibility(View.GONE);
			allchoose.setVisibility(View.VISIBLE);
			del_choose.setVisibility(View.VISIBLE);
			cancle.setVisibility(View.VISIBLE);
			mList.clear();
			for (Bean bean:
			list) {
				bean.setShow(isShow);
			}
			adapter.notifyDataSetChanged();
			break;
		case R.id.allchoose:
			if ("全选".equals(allchoose.getText().toString().trim())) {
				for (Bean bean:
				list) {
					if (!bean.isChecked()) { //未选中的都设置为选中
						bean.setChecked(true);
						if (!mList.contains(bean)) { //选中的没有集合中没有的添加到集合中
							mList.add(bean);
						}
					}
				}
				adapter.notifyDataSetChanged();
				allchoose.setText("多选");
			} else if ("多选".equals(allchoose.getText().toString().trim())) {
				for (Bean bean: list) {
					bean.setChecked(false); //点击多选就把选中的都取消
					if (!mList.contains(bean)) { //集合中没有这个对象就删掉
						mList.remove(bean);
					}
				}
				adapter.notifyDataSetChanged();
				allchoose.setText("全选");
			}
			break;
		case R.id.del_choose:
			if (mList != null && mList.size() > 0) {
				list.removeAll(mList); //集合间的删除要用removeAll
				adapter.notifyDataSetChanged();
				mList.clear();
			} else {
				Toast.makeText(getApplicationContext(), "请选择条目", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.cancle:
			//点击取消改为删除状态
			if (isShow) {
				for (Bean bean:
				list) {
					bean.setShow(false);
					bean.setChecked(false);
					del.setVisibility(View.VISIBLE);
					allchoose.setVisibility(View.GONE);
					del_choose.setVisibility(View.GONE);
					cancle.setVisibility(View.GONE);
				}
				mList.clear();
				adapter.notifyDataSetChanged();
			}
			break;

		default:
			break;
		}
	}

}

 


下面是adapter

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

	private List < Bean > mList = new ArrayList < Bean > ();
	private Context mContext;
	private Bean bean;
	private OnShowChangedListener onShowChangedListener;

	public ListViewAdapter(List < Bean > list, Context context) {
		this.mList = list;
		this.mContext = context;
	}

	@Override public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.item_list, null);
			viewHolder = new ViewHolder();
			viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.ck);
			viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		bean = mList.get(position);
		if (bean.isShow()) {
			viewHolder.checkBox.setVisibility(View.VISIBLE);
		} else {
			viewHolder.checkBox.setVisibility(View.GONE);
		}
		viewHolder.tv.setText(mList.get(position).getName());
		viewHolder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					bean.setChecked(true);
				} else {
					bean.setChecked(false);
				}
				//回调方法,将item加入已选择的
				onShowChangedListener.isShowItemClickListener(bean);
			}
		});
		//设置选中状态
		viewHolder.checkBox.setChecked(bean.isChecked());
		return convertView;
	}

	class ViewHolder {
		private CheckBox checkBox;
		private TextView tv;
	}

	public interface OnShowChangedListener {
		void isShowItemClickListener(Bean bean);
	}

	public void setOnShowChangedListener(OnShowChangedListener onShowChangedListener) {
		this.onShowChangedListener = onShowChangedListener;
	}

}

 

还有一个bean类

 

public class Bean {

	private String name;
	private boolean isShow;
	private boolean isChecked;
	/**
* @return the name
*/
	public String getName() {
		return name;
	}
	/**
* @param name the name to set
*/
	public void setName(String name) {
		this.name = name;
	}
	/**
* @return the isShow
*/
	public boolean isShow() {
		return isShow;
	}
	/**
* @param isShow the isShow to set
*/
	public void setShow(boolean isShow) {
		this.isShow = isShow;
	}
	/**
* @return the isChecked
*/
	public boolean isChecked() {
		return isChecked;
	}
	/**
* @param isChecked the isChecked to set
*/
	public void setChecked(boolean isChecked) {
		this.isChecked = isChecked;
	}

}

activity_main布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.practisemoredel.MainActivity" >
    
    <ListView 
       android:padding="10dp"
       android:divider="@null"
       android:layout_weight="5"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/lv"
       
       />
    <LinearLayout 
       android:background="#89cff0"
       android:layout_weight="1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       >
       <Button 
             android:id="@+id/del"
             android:layout_marginLeft="20dp"
             android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="删除"
       android:gravity="center"
       android:textSize="14sp"
           />
        <Button 
             android:id="@+id/allchoose"
             android:visibility="gone"
             android:layout_marginLeft="20dp"
             android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="全选"
       android:gravity="center"
       android:textSize="14sp"
           />
         <Button 
             android:id="@+id/del_choose"
             android:visibility="gone"
             android:layout_marginLeft="20dp"
             android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="删除"
       android:gravity="center"
       android:textSize="14sp"
           />
          <Button 
             android:id="@+id/cancle"
             android:visibility="gone"
             android:layout_marginLeft="20dp"
             android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="取消"
       android:gravity="center"
       android:textSize="14sp"
           />
    </LinearLayout>
</LinearLayout>

 

item布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
        >
    
    <CheckBox 
       android:visibility="gone"
       android:layout_marginLeft="20dp"
       android:id="@+id/ck"
        android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:focusable="false"
        />
   
    <TextView 
       android:layout_marginLeft="20dp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/tv"
       android:textColor="#000000"
       android:textSize="20sp"
       />
    
</LinearLayout>
</LinearLayout>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值