checkbox的单选全选,反选,计算价格,删除
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:layout_weight="1"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反 选"
android:layout_weight="1"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全不选"
android:layout_weight="1"
/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:layout_weight="1"
/>
</LinearLayout>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ListView
android:id="@+id/listveiw"
android:layout_width="match_parent"
android:layout_height="match_parent" ></ListView> </LinearLayout>
listveiw.xml
如果ListView的Item中包含CheckBox,由于CheckBox的焦点优先级高于ListView,所以当选中ListView时,ListView将不会有选中的状态,解决的方法就是在checkbox组件中加一属性:android:focusable="false"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ddd" /> <CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false"
android:clickable="false" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" /> </RelativeLayout>
MainActivity.java
package com.bawei.day04_listview; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import com.bawei.day04_listview.Adper.ViewHolder;
import com.bawei.vo.Good; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity { private Button button1;
private Button button2;
private Button button3;
private ListView listView;
private List<Good> list;
private TextView price;
private int num=0;
private int pric=0;
private BaseAdapter adapter;
private Button button4;
private List<Good> li;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);//全选
button2 = (Button) findViewById(R.id.button2);//反选
button3 = (Button) findViewById(R.id.button3);//全不选
button4 = (Button) findViewById(R.id.button4);//删除
listView = (ListView) findViewById(R.id.listveiw);//价格 price = (TextView) findViewById(R.id.price);
list = new ArrayList<Good>();
//赋值
for (int i = 0; i < 60; i++) {
list.add(new Good(i+"", false));
}
//适配
adapter = new Adper(list,MainActivity.this);
listView.setAdapter(adapter);
//全选
button1.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub num = 0;
pric = 0; for (int i = 0; i < list.size(); i++) {
//改变boolean
list.get(i).setBo(true);
//如果为选中
if(list.get(i).getBo()){ num++;
pric+=Integer.parseInt(list.get(i).getName()); }
}
//刷新
adapter.notifyDataSetChanged();
//显示
price.setText("一共选了"+num+"件,"+"价格是"+pric+"元"); }
});
//反选
button2.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
num = 0;
pric = 0;
for (int i = 0; i < list.size(); i++) {
//改值
if(list.get(i).getBo()){
list.get(i).setBo(false);
}else{
list.get(i).setBo(true);
}
//刷新
adapter.notifyDataSetChanged();
//如果为选中
if(list.get(i).getBo()){ num++;
pric+=Integer.parseInt(list.get(i).getName()); }
}
// 用TextView显示
price.setText("一共选了"+num+"件,"+"价格是"+pric+"元");
}
});
//全不选
button3.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
num = 0;
pric = 0;
for (int i = 0; i < list.size(); i++) {
//改值
list.get(i).setBo(false);
//刷新
adapter.notifyDataSetChanged();
//如果为选中
if(list.get(i).getBo()){
num++;
pric+=Integer.parseInt(list.get(i).getName());
} }
price.setText("一共选了"+num+"件,"+"价格是"+pric+"元");
}
});
//删除1
/*li = new ArrayList<Good>();
button4.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getBo()){
//把要删除的保存起来
li.add(list.get(i)); } }
//删除
list.removeAll(li);
adapter.notifyDataSetChanged();
num = 0;
pric = 0;
price.setText("一共选了"+num+"件,"+"价格是"+pric+"元");
}
});*/
//删除2
button4.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
//获取list集合对应的迭代器
Iterator it=list.iterator();
while (it.hasNext()) {
//得到对应集合元素
Good g=(Good) it.next();
//判断
if(g.getBo()){
//从集合中删除上一次next方法返回的元素
it.remove();
}
}
//刷新
adapter.notifyDataSetChanged();
num = 0;
pric = 0;
//显示
price.setText("一共选了"+num+"件,"+"价格是"+pric+"元");
}
});
// 绑定listView的监听器
listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// 取得ViewHolder对象
ViewHolder viewHolder = (ViewHolder) arg1.getTag();
// 改变CheckBox的状态
viewHolder.checkBox.toggle();
// 将CheckBox的选中状况记录下来
list.get(arg2).setBo(viewHolder.checkBox.isChecked());
// 调整选定条目
if (viewHolder.checkBox.isChecked() == true) {
num++;
pric+=Integer.parseInt(list.get(arg2).getName());
} else {
num--;
pric-=Integer.parseInt(list.get(arg2).getName());
}
// 用TextView显示
price.setText("一共选了"+num+"件,"+"价格是"+pric+"元");
}
}); } }
List的Vo类Good.java
package com.bawei.vo;
public class Good {
private String name;
private boolean bo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean getBo() {
return bo;
}
public void setBo(boolean bo) {
this.bo = bo;
}
@Override
public String toString() {
return "Good [name=" + name + ", bo=" + bo + "]";
}
public Good(String name, boolean bo) {
super();
this.name = name;
this.bo = bo;
}
public Good() {
super();
}
}
适配Adper.java
package com.bawei.day04_listview; import java.util.List; import com.bawei.vo.Good; import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView; public class Adper extends BaseAdapter {
List<Good> list;
Context context; public Adper(List<Good> list, Context context) {
// TODO Auto-generated constructor stub
this.list=list;
this.context=context; } public int getCount() {
// TODO Auto-generated method stub
return list.size();
} public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
if(convertView==null){
convertView=View.inflate(context, R.layout.listveiw, null);
viewHolder=new ViewHolder();
viewHolder.textView=(TextView) convertView.findViewById(R.id.text);
viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.textView.setText("价格:"+list.get(position).getName()); //显示checkBox
viewHolder.checkBox.setChecked(list.get(position).getBo()); return convertView; }
class ViewHolder{
TextView textView;
CheckBox checkBox;
}
}
checkbox的单选全选,反选,计算价格,删除的更多相关文章
- jQuery实现全选/反选和批量删除
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncod ...
- jQuery全选/反选checkbox
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jquery、js操作checkbox全选反选
全选反选checkbox在实际应用中比较常见,本文有个不错的示例,大家可以参考下 操作checkbox,全选反选//全选 function checkAll() { $('input[name=&qu ...
- jquery、js全选反选checkbox
操作checkbox,全选反选 //全选 function checkAll() { $('input[name="TheID"]').attr("checked&quo ...
- 表单javascript checkbox全选 反选 全不选
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 表单Checkbox全选反选全不选
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 关于Winform下DataGridView中实现checkbox全选反选、同步列表项的处理
近期接手一个winform 项目,虽然之前有.net 的经验,但是对一些控件的用法还不是很熟悉. 这段时间将会记录一些在工作中遇到的坎坷以及对应的解决办法,写出来与大家分享并希望大神提出更好解决方法来 ...
- 页面的checkbox框的全选与反选
if (typeof jQuery == 'undefined') { alert("请先导入jQuery");} else { jQuery.extend({ ...
- Jquery 利用单个复选款(checkbox)实现全选、反选
1 <script type="text/javascript"> $(function(){ //全选 $("#CheckedAll").clic ...
随机推荐
- inconfont 字体库应用
先去注册个号码,好像只可以用新浪微博登录哈,搞一个微博去. 第一就是点上面图标库,选择官方和所有都行. 恩接着点一个图标,他就自己跑到 第二个按钮哪里去了,在点第二个按钮,会出来一个创建项目,随便创建 ...
- POI-HSSF and POI-XSSF - Java API To Access Microsoft Excel Format Files
一.概述 HSSF和XSSF是apache开源项目POI中实现java面向Excel的两个接口.两者的区别在于,HSSF适用于Excel '97(-2007)文档,而XSSF适用于Excel 2007 ...
- [LintCode] Sort Integers II 整数排序之二
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...
- centos7 服务管理
服务脚本位置: /usr/lib/systemd/system (开机不登录就能够运行的服务) /usr/lib/systemd/user (用户登录后才能运行的服务) 服务脚本示例: [ ...
- Window.onload与$(document).ready()的对比
- webform FileUpload控件实例应用 上传图片
首先在根目录下建一个"images"文件: HTML: <form id="form1" runat="server"> < ...
- css渐变颜色在线制作
http://www.colorzilla.com/gradient-editor/
- decode 函数将字符串从某种编码转为 unicode 字符
环境:Ubuntu, Python 2.7 基础知识 这个程序涉及到的知识点有几个,在这里列出来,不详细讲,有疑问的直接百度会有一堆的. 1.urllib2 模块的 request 对像来设置 HTT ...
- Linux vi编辑器的基本命令
vi编辑器的文字说明 模式:命令模式,编辑模式,末行模式. 切换方式:命令模式→i→编辑模式,编辑模式→Esc→命令模式,命令模式→:→末行模式. 功能: 命令模式(Command Mode): 控制 ...
- SQLite核心函数一览
abs(X) abs(X)返回 X 的绝对值. Abs(X) returns NULL if X is NULL. Abs(X) return 0.0 if X is a string or blo ...