Android开发ListView中包含CheckBox(点击item选中CheckBox),并设置其全选反选等功能

要实现ListView中CheckBox的选中并记录,我的做法是自己写一个Adapter,继承于BaseAdapter,ListView中的CheckBox需要将focusable,clickable的属性都设置为false,防止CheckBox将点击事件拦截,在ListView的onItemClick方法中接收点击事件并对数据进行处理。废话不多说,上代码。


先给大家看一下listview中数据的xml




    
	
	
	

然后再看main.xml




    


最后把主代码给大家

package com.zk.listview;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
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;

public class CheckBoxInlistViewActivity extends Activity implements OnItemClickListener {
    
    public static final String CHECK = "checkbox";
    public static final String NAME = "name";
    
    private ListView mListView;
    private MyAdapter mAdapter;
    private ArrayList> mData;
    private Boolean mCheckable = false;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        setContentView(R.layout.main);
        mListView = (ListView) findViewById(R.id.lv);
        mListView.setOnItemClickListener(this);
    }
    
    private void initData() {
        mData = new ArrayList>();
        for (int i = 0; i < 10; i++) {
            HashMap map = new HashMap();
            map.put(NAME, "name " + i);
            map.put(CHECK, mCheckable);
            mData.add(map);
        }
        mAdapter = new MyAdapter(getApplicationContext(), mData);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        HashMap item = mData.get(position);
        String name = (String) item.get(NAME);
        Boolean isChecked = (Boolean) item.get(CHECK);
        item.put(CHECK, !isChecked);
        if (!isChecked) {
            Toast.makeText(getApplicationContext(), name + " 被选中!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), name + " 被取消!", Toast.LENGTH_SHORT).show();
        }
        mAdapter.notifyDataSetChanged();
    }
    
    class MyAdapter extends BaseAdapter {
        
        private ArrayList> mDataSource;
        private Context mContext;
        
        public MyAdapter(Context context, ArrayList> list) {
            this.mContext = context;
            this.mDataSource = list;
        } 

        @Override
        public int getCount() {
            if (mDataSource == null) {
                return 0;
            }
            return mDataSource.size();
        }

        @Override
        public Object getItem(int position) {
            if (mDataSource == null) {
                return new HashMap();
            }
            return mDataSource.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = newView(position, parent);
            } else {
                view = convertView;
            }
            bindView(view,position);
            return view;
        }

        private View newView(int position, ViewGroup parent) {
            View view = null;
            LayoutInflater mInflater = LayoutInflater.from(mContext);
            ViewHolder mViewHolder = new ViewHolder();
            view = mInflater.inflate(R.layout.item, parent, false);
            mViewHolder.textView = (TextView) view.findViewById(R.id.tv);
            mViewHolder.checkBox = (CheckBox) view.findViewById(R.id.cb);
            view.setTag(mViewHolder);
            return view;
        }
        
        private void bindView(View view, int position) {
            HashMap data = mDataSource.get(position);
            String str = (String) data.get(NAME);
            Boolean tag = (Boolean) data.get(CHECK);
            ViewHolder mViewHolder = (ViewHolder) view.getTag();
            mViewHolder.textView.setText(str);
            mViewHolder.checkBox.setChecked(tag);
        }

        
        class ViewHolder {
            public TextView textView;
            public CheckBox checkBox;
        }
        
    }
}

如有不合适的地方,希望大家指正,一起进步

你可能感兴趣的:(Android开发ListView中包含CheckBox(点击item选中CheckBox),并设置其全选反选等功能)