android全选反选结算

2/13/2017来源:iOS开发人气:443

MianActivity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.szg.lianxi.MainActivity">

    <TextView
        android:id="@+id/tv_sum_PRice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选" />

        <Button
            android:id="@+id/bt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="反选" />

        <Button
            android:id="@+id/bt3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全不选" />

        <Button
            android:id="@+id/bt4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="结算" />
    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>


</LinearLayout>

ListView的子布局

<?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/tv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/tv_item_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"
        android:layout_weight="1"/>

    <CheckBox
        android:id="@+id/cb_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"

        />

</RelativeLayout>

bean包的属性把属性定义有参构造

package com.szg.lianxi;



public class cargo {
    private String name;
    private String picPath;
    private int price;


    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public cargo(String name, int price, String picPath, boolean flag) {
        this.name = name;
        this.price = price;
        this.picPath = picPath;
        this.flag = flag;

    }

    private boolean flag;

    public cargo(String name, String picPath, boolean flag) {
        this.name = name;
        this.picPath = picPath;
        this.flag = flag;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPicPath() {
        return picPath;
    }

    public void setPicPath(String picPath) {
        this.picPath = picPath;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}

适配器中的代码

package com.szg.lianxi;

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.TextView;
import android.widget.Toast;

import java.util.ArrayList;



public class CargoAdapter extends BaseAdapter {
    private final Context context;
    private final ArrayList<cargo> cargoArrayList;


    CargoAdapter(Context context, ArrayList<cargo> cargoArrayList) {
        this.context = context;
        this.cargoArrayList = cargoArrayList;
    }

    @Override
    public int getCount() {
        return cargoArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            //找到布局
            convertView = View.inflate(context, R.layout.lv_item, null);
            holder.cb_item = (CheckBox) convertView.findViewById(R.id.cb_item);
            holder.tv_item = (TextView) convertView.findViewById(R.id.tv_item_name);
            holder.tv_price = (TextView) convertView.findViewById(R.id.tv_item_price);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_item.setText(cargoArrayList.get(position).getName());
        holder.cb_item.setChecked(cargoArrayList.get(position).isFlag());
        holder.tv_price.setText(cargoArrayList.get(position).getPrice()+"");
//        holder.cb_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//            Toast.makeText(context,"改变了"+position,Toast.LENGTH_SHORT).show();
//        }
//    });
        //给CheckBox设置点击事件
        holder.cb_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //改变bean包中flag
                if(holder.cb_item.isChecked()){
                    cargoArrayList.get(position).setFlag(true);
                }else{
                    cargoArrayList.get(position).setFlag(false);
                }
                //触发价格改变
                MainActivity mainActivity  = (MainActivity) CargoAdapter.this.context;
                mainActivity.setMoney();
            }
        });

        return convertView;
    }

    class ViewHolder {
        TextView tv_item;
        TextView tv_price;
        CheckBox cb_item;
    }
}

MianActivity中的代码

package com.szg.lianxi;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView listview;
    private CargoAdapter cargoAdapter;
    private TextView tv_sum_price;
    int sum=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview=(ListView) findViewById(R.id.listview);
        findViewById(R.id.bt1).setOnClickListener(this);
        findViewById(R.id.bt2).setOnClickListener(this);
        findViewById(R.id.bt3).setOnClickListener(this);
        findViewById(R.id.bt4).setOnClickListener(this);
       tv_sum_price=(TextView)  findViewById(R.id.tv_sum_price);
        initData();
        cargoAdapter=new CargoAdapter(this,caragolist);
        //设置适配器
        listview.setAdapter(cargoAdapter);
    }
    ArrayList<cargo> caragolist=new ArrayList<>();
    private void initData() {
        for (int i=0;i<50;i++){
            caragolist.add( new cargo("商品"+i,i+1,null,false));
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //全选
            case R.id.bt1:
                //遍历集合改变点击的状态
                for (int i=0;i<caragolist.size();i++){
                    caragolist.get(i).setFlag(true);
//                    sum=sum+caragolist.get(i).getPrice();
                    setMoney();
                }
                //刷新适配器
                cargoAdapter.notifyDataSetChanged();
                break;
            //反选
            case R.id.bt2:
                for (int i=0;i<caragolist.size();i++){
                    //将之前的状态取出,改编成相反状态
                    caragolist.get(i).setFlag(!caragolist.get(i).isFlag());
//                    sum=sum+caragolist.get(i).getPrice();
                    setMoney();

                }
                //刷新适配器
                cargoAdapter.notifyDataSetChanged();
                break;
            //全不选
            case R.id.bt3:
                //遍历集合改变点击的状态
                for (int i=0;i<caragolist.size();i++){
                    caragolist.get(i).setFlag(false);
//                    sum=sum+caragolist.get(i).getPrice();
                    setMoney();

                }
                //刷新适配器
                cargoAdapter.notifyDataSetChanged();
                break;
            //结算
            case R.id.bt4:
//                int sum=0;
//                for (int i=0;i<caragolist.size();i++){
//                    if ((caragolist.get(i).isFlag())){
//                        sum=sum+caragolist.get(i).getPrice();
//                    }
//                }
                showNormalDia();

//                Toast.makeText(this,sum+"",Toast.LENGTH_LONG).show();
                break;
        }
    }
    public void setMoney() {
        int sum=0;
        for (int i=0;i<caragolist.size();i++){
            if ((caragolist.get(i).isFlag())){
                sum=sum+caragolist.get(i).getPrice();
            }
        }
        tv_sum_price.setText("总价格"+sum);
    }
    /*普通的对话框*/
    private void showNormalDia()
    {
        //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getapplicationContext());
        final AlertDialog.Builder normalDia=new AlertDialog.Builder(MainActivity.this);

        for (int i=0;i<caragolist.size();i++){
            if ((caragolist.get(i).isFlag())){
                sum=sum+caragolist.get(i).getPrice();
            }
        }
        normalDia.setIcon(R.mipmap.ic_launcher);
        normalDia.setTitle("结算");
        normalDia.setMessage("价格:"+sum);

        normalDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
//                showClickMessage("确定");
//                setMoney();

//                for (int i=0;i<caragolist.size();i++){
//                    if ((caragolist.get(i).isFlag())){
//                        sum=sum+caragolist.get(i).getPrice();
//                    }
//                }

                Toast.makeText(MainActivity.this,sum+"",Toast.LENGTH_LONG).show();
            }
        });
        normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
//                showClickMessage("取消");
            }
        });
        normalDia.create().show();
    }

}