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

jopen 10年前

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

以下是实现的的一个小Demo

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

 

这是XML ListView 每个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="vertical" >                    <RelativeLayout                android:id="@+id/outpatient_check_hospital"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_marginBottom="5.0dip"                android:layout_marginLeft="12.599976dip"                android:layout_marginRight="12.599976dip"                android:layout_marginTop="5.0dip"                android:gravity="center_vertical"                 android:background="#AAAAAA">                        <LinearLayout                    android:id="@+id/linear_layout_up"                    android:layout_width="fill_parent"                    android:layout_height="wrap_content"                    android:layout_margin="10.0dip"                    android:gravity="center"                    android:orientation="horizontal" >                            <ImageView                        android:layout_width="10dip"                        android:layout_height="10dip"                        android:adjustViewBounds="false" />                            <TextView                        android:id="@+id/tv_device_name"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_weight="2.0"                        android:text="名称"                        android:textColor="#ff323232"                        android:textSize="16.0sp"                        android:typeface="monospace" />                            <CheckBox                        android:id="@+id/checkBox1"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:text="" />                                    </LinearLayout>            </RelativeLayout>                </LinearLayout>  
</div> </div>

 

 

 

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

</div> </div>
    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<Integer,boolean>的键值来记录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<Integer, Boolean> 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<Integer, Boolean>();            // 初始化数据            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<Integer, Boolean> getIsSelected() {            return isSelected;        }            public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {            ListViewAdapter.isSelected = isSelected;        }    }  
</div> </div> 来自:http://blog.csdn.net/qq544529563/article/details/38760357