mainactivity
package com.bwie.test;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
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.ListView;
import android.widget.TextView;
import com.bwie.bean.Goods;
import com.bwie.https.Https;
public class MainActivity extends Activity implements OnClickListener{
private String url = “http://172.17.29.120/localuser/loupengfei/kaoshi/shoppingcar.json“;
private Https https;
private ArrayList list;
private myadapter adapter;
float price =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alist = new ArrayList();
//初始化控件
initview();
//请求数据类
https = new Https();
//开启线程
new mythread().start();
}
private void initview() {
Button fanxuan = (Button) findViewById(R.id.bt_fanxuan);
Button quanxuan = (Button) findViewById(R.id.bt_quanxuan);
sum = (TextView) findViewById(R.id.tv_sum);
lv = (ListView) findViewById(R.id.lv_main);
fanxuan.setOnClickListener(this);
quanxuan.setOnClickListener(this);
}
//线程请求数据
class mythread extends Thread{
@Override
public void run() {
String result = https.getdata(url);
Log.e(“-=-=”, result);
Message msg = new Message();
msg.obj = result;
handler.sendMessage(msg);
}
}
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
String result = (String) msg.obj;
Log.e("-", result);
if(result!=null){
list = new ArrayList<Goods>();
try {
JSONObject json = new JSONObject(result);
JSONArray aar = (JSONArray) json.get("data");
for (int i = 0; i < aar.length(); i++) {
JSONObject jsono = aar.getJSONObject(i);
String title = jsono.getString("title");
Log.e("----", title);
String money = jsono.getString("money");
Log.e("----", money);
list.add(new Goods(title, money, false));
}
Log.e("009", list.toString());
} catch (Exception e) {
// TODO: handle exception
}
//适配器
adapter = new myadapter();
lv.setAdapter(adapter);
}
};
};
private TextView sum;
private ListView lv;
private ArrayList<Integer> alist;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt_fanxuan:{
price =0;
for (int i = 0; i < alist.size(); i++) {
if(alist.get(i)==1){
list.get(i).setBcb(false);
}else {
list.get(i).setBcb(true);
price += Float.parseFloat(list.get(i).getMoney());
sum.setText(""+price);
}
}
adapter.notifyDataSetChanged();
}
break;
case R.id.bt_quanxuan:{
price = 0;
for (int i = 0; i < list.size(); i++) {
list.get(i).setBcb(true);
price += Float.parseFloat(list.get(i).getMoney());
sum.setText(""+price);
}
adapter.notifyDataSetChanged();
}
break;
default:
break;
}
}
//适配器
class myadapter extends BaseAdapter{
public myadapter() {
for (int i = 0; i < list.size(); i++) {
alist.add(0);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView.inflate(MainActivity.this, R.layout.item, null);
//初始化控件
TextView title = (TextView) view.findViewById(R.id.tv_title_item);
CheckBox cb = (CheckBox) view.findViewById(R.id.cb_item);
TextView money = (TextView) view.findViewById(R.id.tv_jiage_item);
title.setText(list.get(position).getTitle());
money.setText(list.get(position).getMoney());
cb.setChecked(list.get(position).isBcb());
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
alist.set(position, 1);
price = 0;
for(int i = 0;i<alist.size();i++){
if(alist.get(i)==1){
price += Float.parseFloat(list.get(i).getMoney());
sum.setText(""+price);
}
}
list.get(position).setBcb(true);
}else{
alist.set(position, 0);
price = 0;
for(int i = 0;i<alist.size();i++){
if(alist.get(i)==1){
price += Float.parseFloat(list.get(i).getMoney());
sum.setText(""+price);
}
list.get(position).setBcb(false);
}
}
}
});
return view;
}
}
}
bean包
package com.bwie.bean;
public class Goods {
private String title;
private String money;
private boolean bcb;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
public boolean isBcb() {
return bcb;
}
public void setBcb(boolean bcb) {
this.bcb = bcb;
}
public Goods(String title, String money, boolean bcb) {
super();
this.title = title;
this.money = money;
this.bcb = bcb;
}
public Goods(String title, String money) {
super();
this.title = title;
this.money = money;
}
@Override
public String toString() {
return “Goods [title=” + title + “, money=” + money + “, bcb=” + bcb + “]”;
}
}
请求数据
package com.bwie.https;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Https {
public String getdata(String url){
String result = “”;
try {
//创建新的连接
URL urls = new URL(url);
//获得连接
HttpURLConnection httpurlconnection = (HttpURLConnection) urls.openConnection();
//获得输入流
InputStream is = httpurlconnection.getInputStream();
if(httpurlconnection.getResponseCode()==200){
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readline = “”;
while((readline=br.readLine())!=null){
result+=readline;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
};
}
main布局