列表,累加价钱

来源:互联网 发布:一带一路大数据宣传片 编辑:程序博客网 时间:2024/03/29 04:56

activity_zong.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ListView        android:id="@+id/l_lv"        android:layout_width="match_parent"        android:layout_height="400dp"        android:layout_weight="1"        />    <LinearLayout         android:id="@+id/l_ll1"        android:layout_below="@id/l_lv"        android:layout_alignParentRight="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal"        >        <CheckBox             android:id="@+id/c_check1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/quanxuan"            />        <TextView             android:id="@+id/t_heji"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/heji"            android:textColor="#f00"            />        <TextView             android:id="@+id/t_heji1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="0"            />        <Button             android:id="@+id/b_jieshu"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="#f00"            android:textSize="12sp"            android:text="@string/jishuan"            />    </LinearLayout></LinearLayout>

ZongActivity.class

public class ZongActivity extends Activity {    private ListView lv;    private Shop s;    public TextView pric;    private BaseAdapter adapter;    int total_price=0;//  public TextView heji1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_zong);        if(!AList.list.isEmpty()){            /*Intent intent=getIntent();            s = (Shop) intent.getSerializableExtra("shop");*/            lv = (ListView) findViewById(R.id.l_lv);//          heji1 = (TextView) findViewById(R.id.t_heji1);            pric = (TextView) findViewById(R.id.t_heji1);            CheckBox check=(CheckBox) findViewById(R.id.c_check1);            Button jiesuan=(Button) findViewById(R.id.b_jieshu);//          AList.list.add(s);            adapter = new mbase1(this, AList.list);            lv.setAdapter(adapter);            check.setOnCheckedChangeListener(new OnCheckedChangeListener() {                @Override                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                    // TODO Auto-generated method stub                    if(isChecked){                        for(int i=0;i<AList.list.size();i++){                            AList.list.get(i).setStata(true);                            adapter.notifyDataSetChanged();                            total_price=total_price+Integer.parseInt(AList.list.get(i).getPrice());                            pric.setText(total_price+"");                        }                    }else{                        for(int i=0;i<AList.list.size();i++){                            AList.list.get(i).setStata(false);                            adapter.notifyDataSetChanged();                            total_price=0;                            pric.setText("0");                        }                    }                }            });            jiesuan.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {                    // TODO Auto-generated method stub                    if("".equals(pric.getText().toString())||pric.getText().toString()==null){                        Toast.makeText(ZongActivity.this, "请勾选", Toast.LENGTH_LONG).show();                    }else{                        Toast.makeText(ZongActivity.this, "成功结算", Toast.LENGTH_LONG).show();                    }                }            });        }    }}

mbase1.class

public class mbase1 extends BaseAdapter {    private Context context;    private ArrayList<Shop> list;    private View view;    private ViewHolder vh;    public mbase1(Context context, ArrayList<Shop> list) {        super();        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 list.get(position);    }    @Override    public long getItemId(int position) {        // TODO Auto-generated method stub        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        // TODO Auto-generated method stub        if(convertView==null){            view = View.inflate(context, R.layout.item1, null);            vh = new ViewHolder();            vh.iv=(ImageView) view.findViewById(R.id.i_i1);            vh.text=(TextView) view.findViewById(R.id.t_text);            vh.r1=(CheckBox) view.findViewById(R.id.r_r1);;            vh.price=(TextView) view.findViewById(R.id.t_price);            view.setTag(vh);        }else{            view=convertView;            vh=(ViewHolder) view.getTag();        }        vh.iv.setImageResource(list.get(position).getImageId());        vh.text.setText(list.get(position).getTitle());        vh.r1.setChecked(list.get(position).getStata());        vh.r1.setTag(position);        vh.price.setText(list.get(position).getPrice());        vh.r1.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                CheckBox r1=(CheckBox) v;                ZongActivity za=(ZongActivity) context;                int  sum=Integer.parseInt(za.pric.getText().toString());                int p= (Integer) vh.r1.getTag() ;                int pr=Integer.parseInt(vh.price.getText().toString());                if(r1.isChecked()){                    sum=sum+pr;                    list.get(p).setStata(true);                    System.out.println(sum);                }else{                    sum=sum-pr;                    list.get(p).setStata(false);                }                za.pric.setText(""+sum);//              mbase1.this.notifyDataSetChanged();            }        });        return view;    }    public class ViewHolder{        private TextView text;        private TextView price;        private ImageView iv;        private CheckBox r1;    }}
0 0