首页 > 代码库 > 设置ListView的item多选

设置ListView的item多选


一,首先定义一个类来关联Adapter和Activity能够记住点击的位置

public class MyViewHoler{
	public CheckBox cb;
	public TextView tv;

}


二,Adapter的写法

package com.example.listviewcheckdemo;

import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
/**
 * 
 * @author LiuZiwei
 * 选列表adapter
 *
 */
public class CheckAdapter extends BaseAdapter{

	public static HashMap<Integer, Boolean> isSelected;
	private Context context = null;
	private List<String> list = null;

	@SuppressLint("UseSparseArrays")
	public CheckAdapter(Context context, List<String> list) {
		this.context = context;
		this.list = list;

		isSelected = new HashMap<Integer, Boolean>();
		for (int i = 0; i < list.size(); i++) {
			isSelected.put(i, false);
		}
	}

	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return list.get(arg0);
	}
	
	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2) {
		MyViewHoler holder = null;

		if (view == null) {
			holder = new MyViewHoler();
			view = LayoutInflater.from(context).inflate(R.layout.check_item, null);
			holder.tv = (TextView) view.findViewById(R.id.item_tv);
			holder.cb = (CheckBox) view.findViewById(R.id.item_cb);

			view.setTag(holder);

		} else {
			
			holder = (MyViewHoler) view.getTag();
			
		}

		holder.tv.setText(list.get(position));			
		holder.cb.setChecked(isSelected.get(position));
		return view;
	}

}



三、Activity里的写法


public class MainActivity extends Activity {
	private ListView listview_check;
	private CheckAdapter adapter;
	/** 初始化数据*/
	private List<String> newList=new ArrayList<String>();
	/** 存储点击选中的数据*/
	private List<String> checkList=new ArrayList<String>();
	
	/** 全选*/
	private Button btn_all;
	/** 反选*/
	private Button btn_clear;
	/** 取消已选*/
	private Button btn_cancel;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listview_check=(ListView) findViewById(R.id.listview);	
		btn_all=(Button) findViewById(R.id.all);
		btn_clear=(Button) findViewById(R.id.clear);
		btn_cancel=(Button) findViewById(R.id.cancel);

		/** 初始化数据*/
		for(int i=0;i<10;i++){
			newList.add("选项"+i);
		}
		adapter=new CheckAdapter(this, newList);
		listview_check.setAdapter(adapter);
		listview_check.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {
				// TODO Auto-generated method stub
				MyViewHoler holder=(MyViewHoler) view.getTag();
				
				holder.cb.toggle();// 在每次获取点击的item时改变checkbox的状态
				
				CheckAdapter.isSelected.put(position, holder.cb.isChecked()); // 同一时候改动map的值保存状态
				
				if (holder.cb.isChecked() == true) {
					checkList.add(newList.get(position));
				} else {
					checkList.remove(newList.get(position));
				}
				Log.e("----", "---------"+checkList.toString());
			}
		});
		/** 全选*/
		btn_all.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				checkList.clear();
				for(int i=0;i<newList.size();i++){
					CheckAdapter.isSelected.put(i,true);
					checkList.add(newList.get(i));
				}
				adapter.notifyDataSetChanged();//注意这一句必须加上,否则checkbox无法正常更新状态
			}
		});
		/** 反选*/
		btn_clear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				for(int i=0;i<newList.size();i++){
					if(CheckAdapter.isSelected.get(i)==false){
						CheckAdapter.isSelected.put(i, true);
						checkList.add(newList.get(i));
					}
					else{
						CheckAdapter.isSelected.put(i, false);
						checkList.remove(newList.get(i));
					}
				}
				adapter.notifyDataSetChanged();
			}
		});
		/** 取消已选*/
		btn_cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				for(int i=0;i<newList.size();i++){
					if(CheckAdapter.isSelected.get(i)==true){
						CheckAdapter.isSelected.put(i, false);
						checkList.remove(newList.get(i));
					}
				}
				adapter.notifyDataSetChanged();
			}
		});
	}