博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ListView的CheckBox实现全部选中/不选中
阅读量:7210 次
发布时间:2019-06-29

本文共 6670 字,大约阅读时间需要 22 分钟。

在Adapter类中定义一个HashMap列表,保存每一行是否被选中:

private static HashMap
isSelected;

可见定义了名为isSelected的HashMap列表,再定义它的getter和setter方法:

public static void setIsSelected(HashMap
isSelected) { MyAdapter.isSelected = isSelected; } public static HashMap
getIsSelected() { return isSelected; }

在Adapter类的构造方法中,将isSelected的所有初始值设置为false:

public MyAdapter(Context context, List
list){ this.context = context; this.list = list; isSelected = new HashMap
(); initData(); } private void initData() { for (int i = 0; i < list.size(); i++) { getIsSelected().put(i, false); } }

在getView方法中,定义CheckBox的点击方法,当CheckBox选中/不选中时设置isSelected的值为true/false;同时根据isSelected的值设置CheckBox是否选中,并显示相应的图标:

mCheckbox = (CheckBox)convertView.findViewById(R.id.chkBox);        mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                // TODO Auto-generated method stub                if (isChecked){                    buttonView.setButtonDrawable(R.drawable.r02);                    isSelected.put(position, true);                }else{                    buttonView.setButtonDrawable(R.drawable.r01);                    isSelected.put(position, false);                }            }                    });        mCheckbox.setChecked(getIsSelected().get(position));        if (getIsSelected().get(position)){            mCheckbox.setButtonDrawable(R.drawable.r02);        }else{            mCheckbox.setButtonDrawable(R.drawable.r01);        }

在主窗体文件中,当点击列表下面的CheckBox时,调用selectAll方法将isSelected的所有值设置为true/false,同时显示相应的图标:

@Override    public void onClick(View v) {        // TODO Auto-generated method stub        switch (v.getId()){        case R.id.chkSelect:            selectAll(chkSelect.isChecked());            break;        case R.id.btnDelete:            break;        default:            break;        }            }
private void selectAll(boolean x){        for (int i=0; i

完整的文件如下。

MainActivity.java:

package com.hzhi.mylistview;import android.support.v7.app.ActionBarActivity;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.ListView;public class MainActivity extends ActionBarActivity implements OnClickListener{        private CheckBox chkSelect;    private Button btnDelete;    private List
mList; private MyAdapter mAdapter; private ListView mListview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); mAdapter = new MyAdapter(MainActivity.this, mList); mListview.setAdapter(mAdapter); } private void initView(){ chkSelect = (CheckBox) findViewById(R.id.chkSelect); btnDelete = (Button) findViewById(R.id.btnDelete); mListview = (ListView) findViewById(R.id.lstView); chkSelect.setOnClickListener(this); btnDelete.setOnClickListener(this); chkSelect.setButtonDrawable(R.drawable.r01); } private void initData(){ mList = new ArrayList
(); for (int i=0; i<10; i++){ mList.add("第" + String.valueOf(i) + "条数据"); } } private void selectAll(boolean x){ for (int i=0; i

MyAdapter.java:

package com.hzhi.mylistview;import java.util.HashMap;import java.util.List;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.CompoundButton;import android.widget.TextView;import android.widget.CompoundButton.OnCheckedChangeListener;public class MyAdapter extends BaseAdapter{        List
list; Context context; private static HashMap
isSelected; public MyAdapter(Context context, List
list){ this.context = context; this.list = list; isSelected = new HashMap
(); initData(); } private void initData() { for (int i = 0; i < list.size(); i++) { getIsSelected().put(i, false); } } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(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 CheckBox mCheckbox; TextView mText; if(convertView == null){ convertView = LayoutInflater.from(context).inflate(R.layout.listitem, null); } mCheckbox = (CheckBox)convertView.findViewById(R.id.chkBox); mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (isChecked){ buttonView.setButtonDrawable(R.drawable.r02); isSelected.put(position, true); }else{ buttonView.setButtonDrawable(R.drawable.r01); isSelected.put(position, false); } } }); mCheckbox.setChecked(getIsSelected().get(position)); if (getIsSelected().get(position)){ mCheckbox.setButtonDrawable(R.drawable.r02); }else{ mCheckbox.setButtonDrawable(R.drawable.r01); } mText = (TextView)convertView.findViewById(R.id.txtText); mText.setText(list.get(position).toString()); return convertView; } public static void setIsSelected(HashMap
isSelected) { MyAdapter.isSelected = isSelected; } public static HashMap
getIsSelected() { return isSelected; }}

 运行效果如下:

选中最下面的CheckBox后,实现全部选中:

转载地址:http://korum.baihongyu.com/

你可能感兴趣的文章
官宣!微软宣布桌面版 Edge将基于Chromium进行开发\n
查看>>
如何学JavaScript
查看>>
挖财架构师:不能从会计角度设计记账App
查看>>
C# 7.3新特性一览
查看>>
.NET Core 2.1 Preview 2带来网络方面的改进
查看>>
又拍云,音视频CDN加速利器
查看>>
Bitbucket Pipelines在Atlassian的Bitbucket云上提供持续交付功能
查看>>
Python数据可视化的10种技能
查看>>
一地鸡毛 OR 绝地反击,2019年区块链发展指南
查看>>
Kafka团队修改KSQL开源许可,怒怼云厂商
查看>>
今夏发布的Terraform 0.12将提供for循环和第一类表达式
查看>>
GitHub使用Electron重写桌面客户端
查看>>
Microsoft发布Azure Data Factory v2可视化工具的公开预览版
查看>>
周下载量过200万的npm包被注入恶意代码,Vue、Node项目恐受影响
查看>>
九大最热门的IT岗位,机器学习竟然不是第一
查看>>
Stack Overflow技术报告给开发者哪些启示
查看>>
Vue.js 由 1 到 2 的旅程 - (2)
查看>>
麻省理工学院研究人员设计出针对幽灵党和熔毁的DAWG方法
查看>>
Spark Summit EU重头戏:TensorFlow、结构化的流和GPU硬件加速
查看>>
Docker 上开发 nodejs
查看>>