标签:
package com.baidu.jisuan;
import java.util.ArrayList;
import java.util.List;
import com.baidu.adapter.MyAdapter;
import com.baidu.adapter.MyAdapter.ViewHolder;
import com.baidu.vo.Goods;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView lv;
private int[] images;
private String[] prices;
private List<Goods> list;
private CheckBox all;
private CheckBox fan;
private MyAdapter myAdapter;
private TextView jiesuan;
int price = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
images = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher };
prices = new String[] { "123", "45", "34", "56", "67", "34", "12",
"123", "45", "34", "56", "67", "34", "12" };
init();
}
private void init() {
// 找到相应的控件
lv = (ListView) findViewById(R.id.lv);
all = (CheckBox) findViewById(R.id.all_check);
fan = (CheckBox) findViewById(R.id.all_fan);
jiesuan = (TextView) findViewById(R.id.text);
list = new ArrayList<Goods>();
for (int i = 0; i < images.length; i++) {
list.add(new Goods(images[i], prices[i], false));
}
myAdapter = new MyAdapter(MainActivity.this, list);
lv.setAdapter(myAdapter);
all.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
/*
* for(Goods temp:list){ temp.setIsbuy(isChecked); }
*/
price = 0;
if (isChecked) {
for (int i = 0; i < list.size(); i++) {
list.get(i).setIsbuy(true);
price += Integer.parseInt(list.get(i).getPrice());
}
} else {
for (int i = 0; i < list.size(); i++) {
list.get(i).setIsbuy(false);
}
}
myAdapter.notifyDataSetChanged();
jiesuan.setText("一共:"+price + ""+"元");
}
});
fan.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
/*
* for (Goods temp : list) { if (temp.getIsbuy()) {
* temp.setIsbuy(false); } else { temp.setIsbuy(true); } }
*/
//选中的话,则计算价格
price = 0;
if (isChecked) {
for (int i = 0; i < list.size(); i++) {
//判断如果选中的话,置为不选中,反之,置为选中
if (list.get(i).getIsbuy()) {
list.get(i).setIsbuy(false);
} else {
list.get(i).setIsbuy(true);
price += Integer.parseInt(list.get(i).getPrice());
}
}
} else {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getIsbuy()) {
list.get(i).setIsbuy(false);
} else {
list.get(i).setIsbuy(true);
price += Integer.parseInt(list.get(i).getPrice());
}
}
}
myAdapter.notifyDataSetChanged();
jiesuan.setText("一共:"+price + ""+"元");
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ViewHolder viewholder = (ViewHolder) view.getTag();
viewholder.box.toggle();
list.get(position).setIsbuy(viewholder.box.isChecked());
if (viewholder.box.isChecked() == true) {
price += Integer.parseInt(list.get(position).getPrice());
} else {
price -= Integer.parseInt(list.get(position).getPrice());
}
jiesuan.setText("一共:"+price + ""+"元");
}
});
}
}
package com.baidu.adapter;
import java.util.List;
import com.baidu.jisuan.R;
import com.baidu.vo.Goods;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter{
Context context;
List<Goods> list;
public MyAdapter(Context context, List<Goods> list) {
// TODO Auto-generated constructor stub
this.context=context;
this.list=list;
}
@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) {
ViewHolder viewHolder;
if(convertView==null){
convertView=convertView.inflate(context, R.layout.activity_item, null);
viewHolder=new ViewHolder();
viewHolder.image=(ImageView) convertView.findViewById(R.id.image);
viewHolder.price=(TextView) convertView.findViewById(R.id.price);
viewHolder.box=(CheckBox) convertView.findViewById(R.id.check);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.price.setText(list.get(position).getPrice());
viewHolder.image.setImageResource(list.get(position).getImage());
viewHolder.box.setChecked(list.get(position).getIsbuy());
return convertView;
}
public class ViewHolder{
TextView text,price;
ImageView image;
public CheckBox box;
}
}
标签:
原文地址:http://www.cnblogs.com/1995yu/p/5391187.html