首页 技术 正文
技术 2022年11月23日
0 收藏 922 点赞 2,251 浏览 5278 个字

本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作。通过一个Demo来展示该功能,选中ListView中的某一项,然后点击Button按钮来显示选中了哪些项。

[1] 程序结构图如下:

Android中ListView结合CheckBox判断选中项

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>

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>

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;
} }

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>();
listlistPerson = 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中ListView结合CheckBox判断选中项

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,992
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844