189 8069 5689

AndroidStudio实现简单购物车功能

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

创新互联公司是一家专注于成都做网站、成都网站制作与策划设计,安岳网站建设哪家好?创新互联公司做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:安岳等地区。安岳做网站价格咨询:13518219792

MainActivity的布局文件



  
    
  

  
  

  

    

      

      

      
    

    

    
  

条目的布局文件




  

  

    

    

    

      

      

        

        

        

          

          

          
        
      
    
  

CartAdapter适配器

public class CaetAdapter extends BaseAdapter {

  private Context context;
  private List> list;
  private HashMap pitchOnMap;

  public HashMap getPitchOnMap() {
    return pitchOnMap;
  }

  public void setPitchOnMap(HashMap pitchOnMap) {
    this.pitchOnMap = pitchOnMap;
  }

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

    pitchOnMap = new HashMap<>();
    for (int i = 0; i < list.size(); i++) {
      pitchOnMap.put(list.get(i).get("id"), 0);
    }
  }

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

  @Override
  public Object getItem(int position) {
    return list.get(position);
  }

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

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    convertView = View.inflate(context, R.layout.item_layout, null);
    final CheckBox checkBox;
    ImageView icon;
    final TextView name, price, num, type, reduce, add;

    checkBox = convertView.findViewById(R.id.check_box);
    icon = convertView.findViewById(R.id.iv_adapter_list_pic);
    name = convertView.findViewById(R.id.tv_goods_name);
    price = convertView.findViewById(R.id.tv_goods_price);
    type = convertView.findViewById(R.id.tv_type_size);
    num = convertView.findViewById(R.id.tv_num);
    reduce = convertView.findViewById(R.id.tv_reduce);
    add = convertView.findViewById(R.id.tv_add);

    name.setText(list.get(position).get("name"));
    price.setText("¥ " + (Integer.valueOf(list.get(position).get("price"))) * (Integer.valueOf(list.get(position).get("count"))));
    type.setText(list.get(position).get("type"));
    num.setText(list.get(position).get("count"));

    if(pitchOnMap.get(list.get(position).get("id"))== 0){
      checkBox.setChecked(false);
    }else{
      checkBox.setChecked(true);
    }

    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(checkBox.isChecked()){
          pitchOnMap.put(list.get(position).get("id"),1);
        }else{
          pitchOnMap.put(list.get(position).get("id"), 0);
        }
        mrefreshPriceInterface.refreshPrice(pitchOnMap);
      }
    });

    //商品数量减
    reduce.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (Integer.valueOf(list.get(position).get("count")) <= 1) {
          Toast.makeText(context, "数量不能再减啦,只能删除!", Toast.LENGTH_SHORT).show();
        } else {
          list.get(position).put("count", (Integer.valueOf(list.get(position).get("count")) - 1) + "");
          notifyDataSetChanged();
        }
        mrefreshPriceInterface.refreshPrice(pitchOnMap);
      }
    });
    //商品数量加
    add.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        list.get(position).put("count", (Integer.valueOf(list.get(position).get("count")) + 1) + "");
        notifyDataSetChanged();
        mrefreshPriceInterface.refreshPrice(pitchOnMap);

      }

    });

    return convertView;
  }

  /**
   * 创建接口
   */
  public interface RefreshPriceInterface {
    /**
     * 把价格展示到总价上
     * @param pitchOnMap
     */
    void refreshPrice(HashMap pitchOnMap);
  }

  /**
   * 定义一个接口对象
   */
  private RefreshPriceInterface mrefreshPriceInterface;

  /**
   * 向外部暴露一个方法
   * 把价格展示到总价上
   * @param refreshPriceInterface
   */
  public void setRefreshPriceInterface(RefreshPriceInterface refreshPriceInterface) {
    mrefreshPriceInterface = refreshPriceInterface;
  }


}

MainActivity

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

  private LinearLayout top_bar;
  private ListView listview;
  private CheckBox all_chekbox;
  private TextView price;
  private TextView delete;
  private TextView tv_go_to_pay;

  private List goodsList;
  private UserDao userDao;
  private List> listmap=new ArrayList<>();
  private CaetAdapter adapter;

   private double totalPrice = 0.00;
  private int totalCount = 0;

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

  private void initView() {
    top_bar = (LinearLayout) findViewById(R.id.top_bar);
    listview = (ListView) findViewById(R.id.listview);
    all_chekbox = (CheckBox) findViewById(R.id.all_chekbox);
    price = (TextView) findViewById(R.id.tv_total_price);
    delete = (TextView) findViewById(R.id.tv_delete);
    tv_go_to_pay = (TextView) findViewById(R.id.tv_go_to_pay);

    all_chekbox.setOnClickListener(this);
    delete.setOnClickListener(this);
    tv_go_to_pay.setOnClickListener(this);

    initDate();
    adapter = new CaetAdapter(MainActivity.this, listmap);
    listview.setAdapter(adapter);
    adapter.setRefreshPriceInterface(this);
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.all_chekbox:
        AllTheSelected();
        break;
      case R.id.tv_delete:
        checkDelete(adapter.getPitchOnMap());
        break;
      case R.id.tv_go_to_pay:
        if(totalCount<=0){
          Toast.makeText(this,"请选择要付款的商品~",Toast.LENGTH_SHORT).show();
        }else{
          Toast.makeText(this,"付款成功",Toast.LENGTH_SHORT).show();
        }
        break;
    }
  }
  /**
   * 数据
   */
  private void initDate() {
    //创建集合
    goodsList = new ArrayList<>();
    //数据库
    userDao = Myapplication.getInstances().getDaoSession().getUserDao();
    userDao.deleteAll();
    //数据源
    for (int i = 0; i < 10; i++) {
      //向数据库存放数据
      User user = new User((long) i,
          "购物车里的第" + (i + 1) + "件商品",
          (i + 20) + "码",
          "10",
          "10");
      userDao.insert(user);
    }
    //从数据库中把数据放到集合中
    goodsList=userDao.loadAll();
    //把结合中的数据放到HashMap集合中
    for(int i=0;i map=new HashMap<>();
      map.put("id",goodsList.get(i).getId()+"");
      map.put("name",goodsList.get(i).getName());
      map.put("type",(goodsList.get(i).getType()));
      map.put("price",goodsList.get(i).getPrice()+"");
      map.put("count",goodsList.get(i).getCount()+"");
      listmap.add(map);
    }
  }

  @Override
  public void refreshPrice(HashMap pitchOnMap) {
    priceControl(pitchOnMap);
  }

  /**
   * 控制价格展示总价
   */
  private void priceControl(Map pitchOnMap){
    totalCount = 0;
    totalPrice = 0.00;
    for(int i=0;i 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

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


文章标题:AndroidStudio实现简单购物车功能
本文URL:http://cdxtjz.cn/article/pscedj.html

其他资讯