资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android实现简单购物车功能

本文实例为大家分享了Android实现购物车功能的具体代码,供大家参考,具体内容如下

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的麻栗坡网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

MainActivity布局:

<?xml version="1.0" encoding="utf-8"?>


  
    
  

  
  

  

    

      

      

      
    

    

    
  



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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements CartAdapter.RefreshPriceInterface ,View.OnClickListener{

  private ListView listView;
  private CheckBox cb_check_all;
  private TextView tv_total_price;
  private TextView tv_delete;
  private TextView tv_go_to_pay;

  private CartAdapter adapter;

  private double totalPrice = 0.00;
  private int totalCount = 0;
  private List> goodsList;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initDate();
  }

  //控制价格展示
  private void priceControl(Map pitchOnMap){
    totalCount = 0;
    totalPrice = 0.00;
    for(int i=0;i pitchOnMap) {
    priceControl(pitchOnMap);
  }

  @Override
  public void onClick(View view) {
    switch (view.getId()){
      case R.id.all_chekbox:
        AllTheSelected();
        break;
      case R.id.tv_go_to_pay:
        if(totalCount<=0){
          Toast.makeText(this,"请选择要付款的商品~",Toast.LENGTH_SHORT).show();
          return;
        }
        Toast.makeText(this,"钱就是另一回事了~",Toast.LENGTH_SHORT).show();
        break;
      case R.id.tv_delete:
        if(totalCount<=0){
          Toast.makeText(this,"请选择要删除的商品~",Toast.LENGTH_SHORT).show();
          return;
        }
        checkDelete(adapter.getPitchOnMap());
        break;
    }
  }

  //删除操作
  private void checkDelete(Map map){
    List> waitDeleteList=new ArrayList<>();
    Map waitDeleteMap =new HashMap<>();
    for(int i=0;i map=adapter.getPitchOnMap();
    boolean isCheck=false;
    boolean isUnCheck=false;
    Iterator iter = map.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry entry = (Map.Entry) iter.next();
      if(Integer.valueOf(entry.getValue().toString())==1)isCheck=true;
      else isUnCheck=true;
    }
    if(isCheck==true&&isUnCheck==false){//已经全选,做反选
      for(int i=0;i();
    for(int i=0;i<10;i++){
      HashMap map=new HashMap<>();
      map.put("id",(new Random().nextInt(10000)%(10000-2900+2900) + 2900)+"");
      map.put("name","购物车里的第"+(i+1)+"件商品");
      map.put("type",(i+20)+"码");
      map.put("price",(new Random().nextInt(100)%(100-29+29) + 29)+"");
      map.put("count",(new Random().nextInt(10)%(10-1+1) + 1)+"");
      goodsList.add(map);
    }

    initView();
  }
}

CartAdapter布局:



  

  

    

    

    

      

      

        

        

        

          

          

          
        
      
    
  



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lipeng
 * 2017/6/5.
 */

public class CartAdapter extends BaseAdapter {

  private Context context;
  private List> dataList;
  private ViewHolder vh;
  private Map pitchOnMap;
  private RefreshPriceInterface refreshPriceInterface;

  public CartAdapter(Context context, List> list){
    this.context=context;
    this.dataList=list;

    pitchOnMap=new HashMap<>();
    for(int i=0;i0){

      if(pitchOnMap.get(dataList.get(position).get("id"))==0)vh.checkBox.setChecked(false);
      else vh.checkBox.setChecked(true);
      HashMap map=dataList.get(position);
      vh.name.setText(map.get("name"));
      vh.num.setText(map.get("count"));
      vh.type.setText(map.get("type"));
      vh.price.setText("¥ "+(Double.valueOf(map.get("price")) * Integer.valueOf(map.get("count"))));

      vh.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          final int index=position;
          if(((CheckBox)view).isChecked())pitchOnMap.put(dataList.get(index).get("id"),1);else pitchOnMap.put(dataList.get(index).get("id"),0);
          refreshPriceInterface.refreshPrice(pitchOnMap);
        }
      });
      vh.reduce.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          final int index=position;
          dataList.get(index).put("count",(Integer.valueOf(dataList.get(index).get("count"))-1)+"");
          if(Integer.valueOf(dataList.get(index).get("count"))<=0){
            //可提示是否删除该商品,确定就remove,否则就保留1
            String deID=dataList.get(index).get("id");
            dataList.remove(index);
            pitchOnMap.remove(deID);
          }
          notifyDataSetChanged();
          refreshPriceInterface.refreshPrice(pitchOnMap);
        }
      });
      vh.add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          final int index=position;
          dataList.get(index).put("count",(Integer.valueOf(dataList.get(index).get("count"))+1)+"");
          if(Integer.valueOf(dataList.get(index).get("count"))>15){
            //15为库存可选择上限
            Toast.makeText(context,"已达库存上限~",Toast.LENGTH_SHORT).show();
            return;
          }
          notifyDataSetChanged();
          refreshPriceInterface.refreshPrice(pitchOnMap);
        }
      });
    }

    return view;
  }

  public Map getPitchOnMap(){
    return pitchOnMap;
  }
  public void setPitchOnMap(Map pitchOnMap){
    this.pitchOnMap=new HashMap<>();
    this.pitchOnMap.putAll(pitchOnMap);
  }

  public interface RefreshPriceInterface{
    void refreshPrice(Map pitchOnMap);
  }
  public void setRefreshPriceInterface(RefreshPriceInterface refreshPriceInterface){
    this.refreshPriceInterface=refreshPriceInterface;
  }

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

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

  @Override
  public int getCount() {
    if (dataList != null) {
      return dataList.size();
    } else {
      return 0;
    }
  }

  class ViewHolder{
    CheckBox checkBox;
    ImageView icon;
    TextView name,price,num,type,reduce,add;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网站栏目:Android实现简单购物车功能
链接分享:http://cdkjz.cn/article/iejspo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220