标签:
1、布局实现的效果
2、找到该布局文件下的所在的activity,实现的效果
package com.bwie.checkbox_all; import java.util.ArrayList; import java.util.Iterator; import com.bwie.adapter.MyAdapter; import com.bwie.vo.User; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private Button all; private Button delete; private Button reverse; private TextView sum_price; private ListView lv; private String[] name = new String[] { "宋静萌", "范丽清", "杨洋", "郑芳蓓", "张丹", "刘孟琦", "张聪聪", "任殊慧", "刘黎伟", "巢乐乐", "廉洪波", "朱帅", "李海洋", "黄校丹", "王涛", "李豆" }; private float[] price = new float[] { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }; private User user; private ArrayList<User> list_user; private MyAdapter adapter; float sum=0;(价格的总价) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化数据(找控件) init(); // 为lv设置数据 setDataForLv(); // 为按钮设置点击监听事件 all.setOnClickListener(this); delete.setOnClickListener(this); reverse.setOnClickListener(this); } private void setDataForLv() { // 创建集合对象 list_user = new ArrayList<User>(); for (int i = 0; i < name.length; i++) { user = new User(name[i], price[i], false); // 将对象添加到集合中 list_user.add(user); } // 为listview设置适配器 adapter = new MyAdapter(MainActivity.this, list_user,sum_price); lv.setAdapter(adapter); } private void init() { // TODO Auto-generated method stub // 找控件 all = (Button) findViewById(R.id.all); delete = (Button) findViewById(R.id.delete); reverse = (Button) findViewById(R.id.reverse); sum_price = (TextView) findViewById(R.id.sum_price); lv = (ListView) findViewById(R.id.lv); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.all://全选按钮(将对象中的boolean赋值为true) sum=0;//每次点击是都现将总价赋值为0,否则会出现累加的效果 // 将复选框设置为true for (int i = 0; i < list_user.size(); i++) { list_user.get(i).setIscheck(true); //计算总价 sum += list_user.get(i).getPrice(); } //为总价textview赋值 sum_price.setText(sum+""); // 重新刷新适配器(实现页面重新展示执行后展示最后的界面) adapter.notifyDataSetChanged(); break; case R.id.delete: //迭代遍历集合对象 Iterator<User> iterator = list_user.iterator(); while(iterator.hasNext()) { User next = iterator.next();//获取每次迭代的对象 boolean ischeck = next.isIscheck();//遍历选中的状态,若是选中的状态,就将选中的 条目删除 if(ischeck) { iterator.remove(); } } adapter.notifyDataSetChanged(); break; case R.id.reverse: //反选 for(int i = 0; i < list_user.size(); i++) { //判断复选框的选中状态 boolean ischeck = list_user.get(i).isIscheck(); if(ischeck) { //为选中的状态就使其为未选中的状态 list_user.get(i).setIscheck(false); //计算总价 sum -= list_user.get(i).getPrice(); }else{ //为未选中的状态就使其为选中的状态 list_user.get(i).setIscheck(true); sum += list_user.get(i).getPrice(); } } sum_price.setText(sum+""); adapter.notifyDataSetChanged(); break; } } }
3、对应的适配器的代码
package com.bwie.adapter;
import java.util.ArrayList;
import java.util.List;
import com.bwie.checkbox_all.R;
import com.bwie.vo.User;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private Context context;
private ViewHolder holder;
private List<User> list;
private float sum;
private TextView sum_price;
private SharedPreferences share;
public MyAdapter(Context context, List<User> list, TextView sum_price) {
this.context = context;
this.list = list;
this.sum_price = sum_price;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// 填充新布局
convertView = View.inflate(context, R.layout.item_layout, null);
holder = new ViewHolder();
// 找控件
holder.name = (TextView) convertView.findViewById(R.id.name_item);
holder.price = (TextView) convertView.findViewById(R.id.price_item);
holder.box = (CheckBox) convertView.findViewById(R.id.checkbox);
// 进行绑定
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// 为找到的控件赋值
holder.name.setText(list.get(position).getName());
holder.price.setText(list.get(position).getPrice() + "");
// 滑动时,checkbox状态改变了(convertView复用导致的),就调用这个方法
holder.box.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
list.get(position).setIscheck(isChecked);
sum = 0;
for (int i = 0; i < list.size(); i++) {
boolean ischeck = list.get(i).isIscheck();
if (ischeck) {
float price = list.get(i).getPrice();
sum += price;
}
}
sum_price.setText(sum + "");
}
});
//为复选框赋值必须写在状态变换监听事件的后面(避免错乱)
holder.box.setChecked(list.get(position).isIscheck());
return convertView;
}
class ViewHolder {
TextView name;
TextView price;
CheckBox box;
}
}
4、JavaBean类
package com.bwie.vo; public class User { private String name; private float price; private boolean ischeck; public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public boolean isIscheck() { return ischeck; } public void setIscheck(boolean ischeck) { this.ischeck = ischeck; } @Override public String toString() { return "User [name=" + name + ", price=" + price + ", ischeck=" + ischeck + "]"; } public User(String name, float price, boolean ischeck) { super(); this.name = name; this.price = price; this.ischeck = ischeck; } public User() { super(); } }
listview中的checkbox实现全选、反选、删除的功能
标签:
原文地址:http://www.cnblogs.com/8023-itxinde/p/5401279.html