码迷,mamicode.com
首页 > 其他好文 > 详细

设置ListView的item多选

时间:2014-07-06 19:11:23      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   java   数据   

bubuko.com,布布扣

一,首先定义一个类来关联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();
			}
		});
	}







设置ListView的item多选,布布扣,bubuko.com

设置ListView的item多选

标签:android   style   blog   http   java   数据   

原文地址:http://www.cnblogs.com/mengfanrong/p/3824910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!