iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android checkbox的listView具体操作方法
  • 864
分享到

Android checkbox的listView具体操作方法

checkbox方法listviewAndroid 2022-06-06 09:06:55 864人浏览 泡泡鱼
摘要

本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作。通过一个Demo来展示该功

本文主要实现在自定义的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>(); 
  
 @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软件编程有所帮助。

您可能感兴趣的文章:Android ListView ImageView实现单选按钮实例android基于ListView和CheckBox实现多选和全选记录的功能Android中ListView绑定CheckBox实现全选增加和删除功能(DEMO)Android MVP模式ListView中嵌入checkBox的使用方法Android编程之listView中checkbox用法实例分析Android中ListView结合CheckBox实现数据批量选择(全选、反选、全不选)Android checkbox的listView(多选,全选,反选)具体实现方法Android在listview添加checkbox实现原理与代码Android中ListView + CheckBox实现单选、多选效果


--结束END--

本文标题: Android checkbox的listView具体操作方法

本文链接: https://www.lsjlt.com/news/26087.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • JdbcTemplate操作数据库的具体方法
    JdbcTemplate操作数据库 准备工作 导入相关的jar包,建个数据库 在Spring配置文件中配置数据库连接池 <!-- 数据库连接池--> ...
    99+
    2024-04-02
  • C++实现WPF动画的具体操作方法
    本篇文章为大家展示了C++实现WPF动画的具体操作方法,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。C++编程语言的应方式非常广泛,可以帮助我们轻松的实现许多功能需求。很多人都习惯使用Blend来帮...
    99+
    2023-06-17
  • IntelliJ IDEA窗口组件具体操作方法
    目录背景IDEA版本Tool windows打开 Tool windows关闭Tool wondowsAppearnaceMain Menu具体方法:背景 IDEA刚接触不久,各种常...
    99+
    2024-04-02
  • Android入门教程之ListView的具体使用详解
    目录ListView 的简单用法定制 ListView 的界面提升 ListView 的运行效率ListView 的点击事件ListView 的简单用法 在布局中加入 ListVie...
    99+
    2024-04-02
  • Python Library中Event的具体操作方案
    这篇文章主要讲解了“Python Library中Event的具体操作方案”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python Library中Event的具体操作方案”吧!这个和 ....
    99+
    2023-06-17
  • 电脑截图具体操作方法是什么
    今天小编给大家分享一下电脑截图具体操作方法是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。电脑截图具体操作方法:方法一:...
    99+
    2023-07-01
  • 白鲨一键装机的具体方法怎么操作
      电脑重装可以帮助我们解决大部分的问题,不过很多人还是不会重装,下面小编就和大家介绍白鲨一键装机的过程步骤吧。  1、提前在官网上下载最新版黑鲨装机大师后,在界面中选择“一键装机”下的“重装系统”,此时黑鲨会自动帮我们检测当前电脑的系统信...
    99+
    2023-07-10
  • 编译Python正则表达式的具体操作方法
    这期内容当中小编将会给大家带来有关编译Python正则表达式的具体操作方法,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。现在我们已经看了一些简单的正则表达式,那么我Python正则表达式在实际应用中如何使...
    99+
    2023-06-17
  • 阿里云服务器迁移具体操作方法
    阿里云服务器迁移是一项重要的IT操作,它可以将一台服务器的数据迁移到另一台服务器上,以保证数据的安全性和稳定性。本文将详细介绍阿里云服务器迁移的具体操作方法。 一、阿里云服务器迁移准备在进行阿里云服务器迁移之前,首先需要确保源服务器和目标服...
    99+
    2023-11-20
    阿里 操作方法 服务器
  • 基于easyui checkbox有哪些操作处理方法
    小编给大家分享一下基于easyui checkbox有哪些操作处理方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!1、获取已勾...
    99+
    2024-04-02
  • VB.NET类添加方法的具体操作技巧是什么
    今天就跟大家聊聊有关VB.NET类添加方法的具体操作技巧是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。VB.NET是一门面向对象的编程语言。要想熟练掌握这一语言的应用,需要我们...
    99+
    2023-06-17
  • 配置Windows 7/8系统搜索的具体操作方法
    1、Win7下面:搜索栏输入search ===>点击 Indexing Options 2、Win8下面 先点下搜索框,激活搜 高级选项--->改变索引位置 点击修改按钮,索引位置上面要搜索的地方打勾 ...
    99+
    2023-05-31
    Windows7 Windows8 搜索 系统 Windows 方法
  • pytorch实现textCNN的具体操作
    1. 原理 2014年的一篇文章,开创cnn用到文本分类的先河。 Convolutional Neural Networks for Sentence Classification ...
    99+
    2024-04-02
  • QtQFile文件操作的具体使用
    目录QFile文件操作QFile+QTextStreamQFile+QDataStream很多应用程序都需要具备操作文件的能力,包括对文件内容进行读/写、创建和删除文件等,甚至某些应...
    99+
    2024-04-02
  • Typora自动编号的具体操作
    概述 在使用Typora写比较长的文章时,需要给章节编号,方便区分层次。如果手动编号,一旦章节顺序改变,很多章节的编号都需要一一手动修改,极其麻烦。 Typora官方提供了自动编号的...
    99+
    2024-04-02
  • 解决sparkiv的具体操作步骤
    解决SparkIV的具体操作步骤如下:1. 首先,从互联网上下载并安装SparkIV工具。可以在网上搜索“SparkIV下载”来找到...
    99+
    2023-08-22
    sparkiv
  • 解决archiveofourown的具体操作步骤
    要解决archiveofourown的问题,以下是具体的操作步骤:1. 打开archiveofourown的网站:在浏览器中输入"a...
    99+
    2023-08-23
    archiveofourown
  • Android中ListView的使用方法有哪些
    在Android中,可以使用以下几种方法来使用ListView:1. 使用ArrayAdapter:可以使用ArrayAdapter...
    99+
    2023-08-16
    Android ListView
  • Android之ListView的使用方法有哪些
    在Android中,有以下几种常见的ListView的使用方法:1. 使用ArrayAdapter:通过创建一个ArrayAdapt...
    99+
    2023-08-12
    Android ListView
  • Android ListView列表优化的方法详解
    1. 使用ViewHolder模式:该模式可以减少findViewById的调用次数。在getView()方法中,通过ViewHol...
    99+
    2023-08-14
    Android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作