Android checkbox的listView具体操作方法
作者:cjjky 发布时间:2023-10-10 06:58:33
标签:Android,checkbox,listView
本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作。通过一个Demo来展示该功能,选中ListView中的某一项,然后点击Button按钮来显示选中了哪些项。
1、程序结构图如下:
其中Person.java是实体类,MainActivity.java是Activity组件类。listitem.xml是自定义的列表每项布局文件。
2、listitem.xml布局文件源码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="@+id/list.select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/list.name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:layout_gravity="center"
android:textSize="20dp"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/list.address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Address"
android:layout_gravity="center"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
3、 main.xml布局文件源码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show"/>
<ListView
android:id="@+id/lvperson"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
4、Person.java实体类源码如下:
package com.andyidea.bean;
public class Person {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
5、MainActivity.java类源码如下:
package com.andyidea.listview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.andyidea.bean.Person;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
Button show;
ListView lv;
List<Person> persons = new ArrayList<Person>();
Context mContext;
MyListAdapter adapter;
List<Integer> listItemID = new ArrayList<Integer>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = getApplicationContext();
show = (Button)findViewById(R.id.show);
lv = (ListView)findViewById(R.id.lvperson);
initPersonData();
adapter = new MyListAdapter(persons);
lv.setAdapter(adapter);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listItemID.clear();
for(int i=0;i<adapter.mChecked.size();i++){
if(adapter.mChecked.get(i)){
listItemID.add(i);
}
}
if(listItemID.size()==0){
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
builder1.setMessage("没有选中任何记录");
builder1.show();
}else{
StringBuilder sb = new StringBuilder();
for(int i=0;i<listItemID.size();i++){
sb.append("ItemID="+listItemID.get(i)+" . ");
}
AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
builder2.setMessage(sb.toString());
builder2.show();
}
}
});
}
/**
* 模拟数据
*/
private void initPersonData(){
Person mPerson;
for(int i=1;i<=12;i++){
mPerson = new Person();
mPerson.setName("Andy"+i);
mPerson.setAddress("GuangZhou"+i);
persons.add(mPerson);
}
}
//自定义ListView适配器
class MyListAdapter extends BaseAdapter{
List<Boolean> mChecked;
List<Person> listPerson;
HashMap<Integer,View> map = new HashMap<Integer,View>();
public MyListAdapter(List<Person> list){
listPerson = new ArrayList<Person>();
listPerson = list;
mChecked = new ArrayList<Boolean>();
for(int i=0;i<list.size();i++){
mChecked.add(false);
}
}
@Override
public int getCount() {
return listPerson.size();
}
@Override
public Object getItem(int position) {
return listPerson.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder = null;
if (map.get(position) == null) {
Log.e("MainActivity","position1 = "+position);
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.listitem, null);
holder = new ViewHolder();
holder.selected = (CheckBox)view.findViewById(R.id.list_select);
holder.name = (TextView)view.findViewById(R.id.list_name);
holder.address = (TextView)view.findViewById(R.id.list_address);
final int p = position;
map.put(position, view);
holder.selected.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox)v;
mChecked.set(p, cb.isChecked());
}
});
view.setTag(holder);
}else{
Log.e("MainActivity","position2 = "+position);
view = map.get(position);
holder = (ViewHolder)view.getTag();
}
holder.selected.setChecked(mChecked.get(position));
holder.name.setText(listPerson.get(position).getName());
holder.address.setText(listPerson.get(position).getAddress());
return view;
}
}
static class ViewHolder{
CheckBox selected;
TextView name;
TextView address;
}
}
程序运行后的结果如下:
希望本文所述对大家学习Android软件编程有所帮助。


猜你喜欢
- 一、背景知识:树(Tree)在之前的笔记中,我们介绍的链表、栈、队列、数组和字符串都是以线性结构来组织数据的。本篇笔记要介绍的树采用的是树状
- 本文实例为大家分享了Java编写实现九宫格应用的具体代码,供大家参考,具体内容如下在九宫格里面轮流画圈或叉,哪一方先在水平、竖直、或对角线上
- 最近有个需求,需要统计APP的在线人数,其实以前也统计过,采取的是上线发送一个请求$this->cache->incr()加1,
- 前情提要本文中提供了九种方式获取resources目录下文件的方式。其中打印文件的方法如下: /**
- 网络唤醒实现了对网络的集中管理,即在任何时刻,网管中心的IT管理人员可以经由网络远程唤醒一台处于休眠或关机状态的计算机。使用这一功能,IT管
- /* * 获取当前的手机号 &nb
- jsoup是一个非常好用的html解析工具。使用时需要下载相应的jar包。下面就是我使用jsoup解析html的表格的java源
- JAVA中Integer类下的常用方法有哪些?1.进制转换 n进制转10进制 字符串结果Integer.parseInt(String s,
- java获取文件的inode标识符,如果文件被删除或者重命名,inode的值会发生变更,因此可以在第一次加载File之后记录inode,后续
- JFrame默认的窗体比较土,可以通过一定的美化,让窗体表现的比较漂亮,具体要根据设计的设计图进行美化;JFrame美化的大致思路:先将JF
- 这篇文章主要介绍了java多线程加锁以及Condition类的使用实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- java 单例模式的实例详解概念: java中单例模式是一种常见的设计模式,单例模式分三种:懒汉式单例、饿汉式单例、登记式单例三
- 1、前言最近在用Kotlin+Spring Boot写一个后端项目,实体类习惯性地用了Kotlin中的data class,但是Spring
- 本文实例为大家分享了ActionBar实现tab导航效果的具体代码,供大家参考,具体内容如下先来说一说基础知识:一、基本使用方法1.获取Ac
- 尽管Java提供了一个可以处理文件的IO操作类。 但是没有一个复制文件的方法。 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时
- 两个简单的例子,代码实现如下:1、随机拆分一个整数public static List<Integer> randomList(
- 公钥与私钥公钥与私钥是成对的,一般的,我们认为的是公钥加密、私钥解密、私钥签名、公钥验证,有人说成私钥加密,公钥解密时不对的。公钥与私钥的生
- 前言众所周知,在多个项目中可能会相同的模块,如果每个项目都去创建一遍的话,这样开发效率会很低。比如在开发一个APP应用的时候,有供APP使用
- Application.Idle()方法表示:当应用程序处于空闲状态时执行相应代码。示例程序1、界面设计:一个简单的Lable控件2、代码u
- 前言本文主要给大家介绍了关于C/C++混合编程extern “C”使用的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介