购物车

package com.example.listviewproject;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.listviewproject.bean.One;
import com.example.listviewproject.utils.HttpUitls;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
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.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

	private One mOne;
	private List<One> mList;
	private ListView mlv;
	private BaseAdapter adapter;
	private ImageLoader loader;
	private ToggleButton but1;
	private Button but2;
	private float price2;
	private float price3;
	private TextView account;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mlv = (ListView) findViewById(R.id.mListView);
		account=(TextView)findViewById(R.id.account);
		but1 = (ToggleButton) findViewById(R.id.but1);
		but2 = (Button) findViewById(R.id.but2);
		mList = new ArrayList<One>();
		//解析数据
		startThread();
		//listView方法
		lvfind();
		//监听方法
		Listener();
	}
	//通用结账方法
	private void accountmethod() {
		//循环mlist
		for (int i = 0; i < mList.size(); i++) {
			if(mList.get(i).isCheck()==true){
				String p1 = mList.get(i).getPrice();
				//强转String类型为float
				float price1=Float.parseFloat(p1);
				price2=price2+price1;
			}
		}
		account.setText(price2+"");
		price2=0;
		}
	//按钮监听事件
	private void Listener() {
		
		//全选反全选按钮
		but1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			private float price3;
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (but1.isChecked() == true) {
					for (int i = 0; i < mList.size(); i++) {
						mList.get(i).setCheck(true);
						String p1 = mList.get(i).getPrice();
						float price1=Float.parseFloat(p1);
						price2=price2+price1;
					}
					account.setText(price2+"");
					price2=0;
				} else {
					for (int i = 0; i < mList.size(); i++) {
						mList.get(i).setCheck(false);
						float price1=(float) 0.00;
						account.setText(price1+"");
					}
				}
				adapter.notifyDataSetChanged();
			}
		});
		//反选按钮
		but2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int num=0;
				for (int i = 0; i < mList.size(); i++) {
					if (mList.get(i).isCheck() == true) {
						mList.get(i).setCheck(false);
					} else if (mList.get(i).isCheck() == false) {
						mList.get(i).setCheck(true);
					}
				}
				for (int i = 0; i < mList.size(); i++) {
					if(mList.get(i).isCheck()==true){
						num++;
					}
				}
				accountmethod();
				if(num==mList.size()){
					but1.setChecked(true);
				}else{
					but1.setChecked(false);
				}
				adapter.notifyDataSetChanged();
			}
		});
	}

	private void lvfind() {
		//适配器
		loader = ImageLoader.getInstance();
		loader.init(ImageLoaderConfiguration.createDefault(MainActivity.this));
		adapter = new BaseAdapter() {

			@Override
			public View getView(final int position, View convertView,
					ViewGroup parent) {
				final holder ho;
				if (convertView == null) {
					convertView = View.inflate(MainActivity.this,
							R.layout.item, null);
					ho = new holder();
					ho.tv1 = (TextView) convertView.findViewById(R.id.tv01);
					ho.tv2 = (TextView) convertView.findViewById(R.id.tv02);
					ho.ho = (ImageView) convertView.findViewById(R.id.image);
					ho.cb1 = (CheckBox) convertView.findViewById(R.id.cb);
					ho.cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						
						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							// TODO Auto-generated method stub
							if (isChecked == false) {
								mList.get(position).setCheck(false);
							} else if (isChecked == true) {
								mList.get(position).setCheck(true);
							}
							accountmethod();
						}
					});
					convertView.setTag(ho);
				} else {
					ho = (holder) convertView.getTag();
				}
				ho.tv1.setText(mList.get(position).getName());
				ho.tv2.setText(mList.get(position).getPrice());
				loader.displayImage(mList.get(position).getImg1(), ho.ho);
				ho.cb1.setChecked(mList.get(position).isCheck());
				return convertView;
			}

			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return position;
			}

			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return position;
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return mList.size();
			}

			class holder {
				ImageView ho;
				TextView tv1;
				TextView tv2;
				CheckBox cb1;
			}
		};
		Log.i("lege", "我爱范勤");
		mlv.setAdapter(adapter);
	}

	private void startThread() {
		new Thread() {

			public void run() {
				try {
					String qudu = HttpUitls
							.read("http://fun.51fanli.com/api/taohuasuan/getHotItems/?c_src=5&cids=9000&page=1&size=10");
					Log.e("HttpUitls", qudu);
					JSONObject obj = new JSONObject(qudu);

					JSONObject data = obj.optJSONObject("data");
					JSONArray items = data.optJSONArray("items");

					for (int i = 0; i < items.length(); i++) {
						JSONObject goods = items.getJSONObject(i);

						String name = goods.optString("name");
						String price = goods.optString("price");
						String img1 = goods.optString("img1");
						// Log.e("One", name+price+img1);
						mOne = new One(price, img1, name, false);
						Log.e("One", mOne.toString());
						mList.add(mOne);
					}
				} catch (JSONException e) {
					e.printStackTrace();
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			};
		}.start();
	}

}



//工具包

package com.example.listviewproject.utils;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpUitls {
    public static String read(String urlstr) throws Exception{
        
        HttpGet get=new HttpGet(urlstr);
        HttpClient client =new DefaultHttpClient();
        HttpResponse execute = client.execute(get);
        int code = execute.getStatusLine().getStatusCode();
        if(HttpStatus.SC_OK==code){
            String str = EntityUtils.toString(execute.getEntity(),"utf-8");
            return str;
        }
        return "";
        
    }
}

//bean类


package com.example.listviewproject.bean;

public class One {

    private String price;
    private String img1;
    private String name;
    private boolean check;
    @Override
    public String toString() {
        return "One [price=" + price + ", img1=" + img1 + ", name=" + name
                + ", check=" + check + "]";
    }
    public One() {
        super();
        // TODO Auto-generated constructor stub
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getImg1() {
        return img1;
    }
    public void setImg1(String img1) {
        this.img1 = img1;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public boolean isCheck() {
        return check;
    }
    public void setCheck(boolean check) {
        this.check = check;
    }
    public One(String price, String img1, String name, boolean check) {
        super();
        this.price = price;
        this.img1 = img1;
        this.name = name;
        this.check = check;
    }
    

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值