Android中ListView与CheckBox结合----多选与记录
来源:互联网 发布:剑三成男脸型数据大全 编辑:程序博客网 时间:2024/05/20 06:26
在实际项目中用到了这个功能,所以查了众多同行的帖子,,发现这位小伙伴的最符合我的需求,所以转载过来。实际应用中我还加入了一些个人的特殊处理,如果有问题大家可以在评论区留言交流。
很多时候我们会用到ListView与CheckBox结合的东西,比如一个清单,可以用来多选,但是很多人似乎也在这个地方遇到很多问题,我刚开始学的时候也是遇到假选问题,当列表中数量多的之后,我勾选一个,滑动页面会发现条目也勾选上了, 这明显与我们的要求不符合,后来网上找了找资料,用HashMap来记录一个CheckBox的勾选记录就解决了
以下是实现的的一个小Demo
这是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>
这是显示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() {
-
- 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;
-
-
- private static HashMap<Integer, Boolean> isSelected;
-
- class ViewHolder {
-
- TextView tvName;
- CheckBox cb;
- }
-
- public ListViewAdapter(Context context, String[] beans) {
-
- this.beans = beans;
- this.context = context;
- isSelected = new HashMap<Integer, Boolean>();
-
- initDate();
- }
-
-
- private void initDate() {
- for (int i = 0; i < beans.length; i++) {
- getIsSelected().put(i, false);
- }
- }
-
- @Override
- public int getCount() {
-
- return beans.length;
- }
-
- @Override
- public Object getItem(int position) {
-
- return beans[position];
- }
-
- @Override
- public long getItemId(int position) {
-
- return position;
- }
-
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
-
-
- 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 = (ViewHolder) convertView.getTag();
- }
-
- holder.tvName.setText(bean);
-
- 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);
- }
-
- }
- });
-
-
- 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;
- }
- }
需要的朋友直接拷贝修改就可以了
0 0