Android checkbox的listView具体操作方法

作者:cjjky 时间:2023-10-10 06:58:33 

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

Android checkbox的listView具体操作方法

其中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 checkbox的listView具体操作方法

希望本文所述对大家学习Android软件编程有所帮助。

标签:Android,checkbox,listView
0
投稿

猜你喜欢

  • 在Android app中实现九(n)宫格图片连续滑动效果

    2022-10-14 21:09:23
  • Spring Bean创建流程分析讲解

    2022-04-20 22:53:07
  • mybatis @InsertProvider报错问题及解决

    2023-09-27 15:49:59
  • Windows编写jar启动脚本和关闭脚本的操作方法

    2021-05-28 04:36:58
  • Spring interceptor拦截器配置及用法解析

    2023-06-26 06:08:15
  • 如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    2023-02-12 21:25:59
  • Java 数据结构与算法系列精讲之单向链表

    2023-07-10 08:22:12
  • Mybatis-Plus的SQL语句组拼原理说明

    2021-07-03 20:39:22
  • spring-cloud入门之eureka-client(服务注册)

    2023-12-16 22:42:51
  • 深入解析Java多态进阶学习

    2022-05-16 16:06:55
  • SpringBoot 如何实时刷新静态文件

    2023-11-02 13:27:09
  • Android开发之Android.mk模板的实例详解

    2022-02-20 09:11:44
  • Java实现Dijkstra输出最短路径的实例

    2023-09-01 17:44:02
  • Map与JavaBean相互转换的工具类 

    2021-09-22 20:59:02
  • htmlcleaner使用方法及xpath语法初探

    2023-04-11 07:14:06
  • spring boot多数据源动态切换代码实例

    2022-03-11 00:37:09
  • Android性能优化之线程监控与线程统一详解

    2023-11-24 07:38:50
  • 你都理解创建线程池的参数吗?

    2022-06-10 06:36:05
  • IDEA2020.1使用LeetCode插件运行并调试本地样例的方法详解

    2022-02-28 09:44:47
  • WPF实现手风琴式轮播图切换效果

    2022-01-24 13:49:26
  • asp之家 软件编程 m.aspxhome.com