Android中ListView与CheckBox结合----多选与记录

很多时候我们会用到ListView与CheckBox结合的东西,比如一个清单,可以用来多选,但是很多人似乎也在这个地方遇到很多问题,我刚开始学的时候也是遇到假选问题,当列表中数量多的之后,我勾选一个,滑动页面会发现条目也勾选上了, 这明显与我们的要求不符合,后来网上找了找资料,用HashMap来记录一个CheckBox的勾选记录就解决了

以下是实现的的一个小Demo

Android中ListView与CheckBox结合----多选与记录_第1张图片


这是XML ListView 每个item文件清单




    

        

            

            

            
            
        
    




这是显示ListView的页面,简单初始化几个数据

public class MainActivity extends Activity {

	private ListView listView;
	private ListViewAdapter adapter;
	private String[] beans = new String[] { "1", "2", "3", "4", "5", "6", "7",
			"8", "9", "10", "11", "12", "13","14","15","16","17","18","19" };

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

		initView();
	}
	
	private void initView() {
		// TODO Auto-generated method stub
		Log.i("htp", "beans.size:" + beans.length);
		listView = (ListView) findViewById(R.id.listView1);
		adapter = new ListViewAdapter(MainActivity.this, beans);
		listView.setAdapter(adapter);
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}



下面就写一个Adapter类,我们依然继承BaseAdapter类。这里我们使用一个HashMap的键值来记录checkbox在对应位置的选中状况

package com.example.listviewcheckboxdemo;

import java.util.HashMap;
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;

public class ListViewAdapter extends BaseAdapter {

	private Context context;
	private String[] beans;

	// 用来控制CheckBox的选中状况
	private static HashMap isSelected;

	class ViewHolder {

		TextView tvName;
		CheckBox cb;
	}

	public ListViewAdapter(Context context, String[] beans) {
		// TODO Auto-generated constructor stub
		this.beans = beans;
		this.context = context;
		isSelected = new HashMap();
		// 初始化数据
		initDate();
	}

	// 初始化isSelected的数据
	private void initDate() {
		for (int i = 0; i < beans.length; i++) {
			getIsSelected().put(i, false);
		}
	}

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

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return beans[position];
	}

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

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		// 页面
		ViewHolder holder;
		String bean = beans[position];
		LayoutInflater inflater = LayoutInflater.from(context);
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.assist_device_binding_list_item, null);
			holder = new ViewHolder();
			holder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
			holder.tvName = (TextView) convertView
					.findViewById(R.id.tv_device_name);
			convertView.setTag(holder);
		} else {
			// 取出holder
			holder = (ViewHolder) convertView.getTag();
		}

		holder.tvName.setText(bean);
		// 监听checkBox并根据原来的状态来设置新的状态
		holder.cb.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				if (isSelected.get(position)) {
					isSelected.put(position, false);
					setIsSelected(isSelected);
				} else {
					isSelected.put(position, true);
					setIsSelected(isSelected);
				}

			}
		});

		// 根据isSelected来设置checkbox的选中状况
		holder.cb.setChecked(getIsSelected().get(position));
		return convertView;
	}

	public static HashMap getIsSelected() {
		return isSelected;
	}

	public static void setIsSelected(HashMap isSelected) {
		ListViewAdapter.isSelected = isSelected;
	}
}

需要的朋友直接拷贝修改就可以了

你可能感兴趣的:(android学习笔记)