checkbox的单选全选,反选,计算价格,删除

时间:2021-03-07 16:17:21

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