登录 立即注册
安币:

楼主: 应用安卓

Android实现购物车功能,android购物车源码

[复制链接]

451

主题

1005

帖子

1974

安币

手工艺人

发表于 2018-1-23 09:56:43 | 显示全部楼层 |阅读模式

            

        最近看了一些淘宝购物车的demo,于是也写了一个。

        效果图如下:

        

201625111341347.jpg

        主要代码如下:
actvity中的代码:

        

[Java] 查看源文件 复制代码
public class shoppingcartactivity extends baseactivity {

  private list<test> data;
  private listview mlistview;
  private shoppingcartadapter adapter;

  private relativelayout rlrefresh;
  private textview tvrefresh;
  private progressbar barrefresh;
  private linearlayout clear;
  private checkbox checkbox_select_all;
  private checkbox checkbox_add;
  private textview integral_sum;
  private int sum = 0;
  private int[] sumintegral;
  private context context;

  @override
  protected void oncreate(bundle bundle) {
    // todo auto-generated method stub
    super.oncreate(bundle);
    setcontentview(r.layout.activity_shopping_cart);
    initview();
  }

  private void initview() {
    context = this;
    showpage = 1;
    ispermitflag = true;
    data = new arraylist<test>();
    // 测试数据
    data.add(new test("id", "color", "type", "100"));
    data.add(new test("id", "color", "type", "200"));
    data.add(new test("id", "color", "type", "300"));
    data.add(new test("id", "color", "type", "0"));
    data.add(new test("id", "color", "type", "300"));
    data.add(new test("id", "color", "type", "100"));
    data.add(new test("id", "color", "type", "500"));
    data.add(new test("id", "color", "type", "0"));
    data.add(new test("id", "color", "type", "900"));

    adapter = new shoppingcartadapter(context, handler, data);

    sumintegral = new int[data.size() + 1];

    checkbox_add = (checkbox) findviewbyid(r.id.checkbox_add);
    integral_sum = (textview) findviewbyid(r.id.integral_sum);
    clear = (linearlayout) findviewbyid(r.id.clear);
    clear.setonclicklistener(new onclicklistener() {

      @override
      public void onclick(view v) {
        data.clear();
        adapter.notifydatasetchanged();
        integral_sum.settext(0 + "");
        checkbox_select_all.setchecked(false);
        checkbox_add.setclickable(false);
      }
    });
    checkbox_select_all = (checkbox) findviewbyid(r.id.checkbox_select);
    checkbox_select_all.setonclicklistener(new onclicklistener() {

      @override
      public void onclick(view v) {

        hashmap<integer, boolean> isselected = shoppingcartadapter
            .getisselected();
        iterator iterator = isselected.entryset().iterator();
        list<boolean> array = new arraylist<boolean>();//列表中checkbox选中状态
        list<integer> nums = new arraylist<integer>();//列表中商品数量
        while (iterator.hasnext()) {
          hashmap.entry entry = (hashmap.entry) iterator.next();
          integer key = (integer) entry.getkey();
          boolean val = (boolean) entry.getvalue();
          array.add(val);
        }
        for (int i = 0; i < data.size(); i++) {
          int num = data.get(i).getnum();
          int integral = integer.valueof(data.get(i).getintegral());
          nums.add(num);
        }
        if (checkbox_select_all.ischecked()) {

          for (int i = 0; i < data.size(); i++) {
            shoppingcartadapter.getisselected().put(i, true);
          }
          checkbox_add.setchecked(true);
          adapter.notifydatasetchanged();
        } else {
          for (int i = 0; i < data.size(); i++) {
            shoppingcartadapter.getisselected().put(i, false);
          }
          checkbox_add.setchecked(false);
          adapter.notifydatasetchanged();
          integral_sum.settext(0 + "");
        }

      }
    });
    mlistview= (listview) findviewbyid(r.id.finance_list);
    mlistview.setadapter(adapter);
    mlistview.setonitemclicklistener(new onitemclicklistener() {

      @override
      public void onitemclick(adapterview<

        actvity中xml的代码:

        

[Java] 查看源文件 复制代码
<

        -xml中头部可以到网上找一个这里就不放上来了
.checkbox和button的样式可以根据个人喜好设置。

        adaper中的代码:

        

[Java] 查看源文件 复制代码
public class shoppingcartadapter extends baseadapter {

  private context context;
  private list<test> loans;
  private layoutinflater inflater;
  private static hashmap<integer, boolean> isselected;
  private static hashmap<integer, integer> numbers;
  private handler handler;
  int num;// 商品数量

  static class viewholder { // 自定义控件集合
    public checkbox ck_select;
    public imageview pic_goods;
    public textview id_goods;
    public textview color_goods;
    public textview type_goods;
    public textview integral_goods;
    public addminuswidget add_minus;
    public linearlayout layout;
    public textview number;
    public button minus;
    public button plus;
  }

  /**
   * 实例化adapter
   * 
   * @param context
   * @param data
   */
  public shoppingcartadapter(context context, handler handler, list<test> data) {
    this.context = context;
    this.inflater = layoutinflater.from(context);
    this.loans = data;
    this.handler = handler;
    isselected = new hashmap<integer, boolean>();
    numbers = new hashmap<integer, integer>();
    initdate();
  }

  private void initdate() {
    for (int i = 0; i < loans.size(); i++) {
      getisselected().put(i, false);
      getnumbers().put(i, 1);
    }
  }

  @override
  public int getcount() {
    return loans.size();
  }

  @override
  public object getitem(int position) {
    return loans.get(position);
  }

  @override
  public long getitemid(int position) {
    return position;
  }

  @override
  public view getview(final int position, view convertview, viewgroup parent) {
    // 自定义视图
    viewholder itemview = null;
    if (convertview == null) {
      // 获取list_item布局文件的视图
      itemview = new viewholder();
      convertview = inflater.inflate(r.layout.list_shopping_cart_item,
          null);
      // 获取控件对象
      itemview.ck_select = (checkbox) convertview
          .findviewbyid(r.id.ck_select);
      itemview.pic_goods = (imageview) convertview
          .findviewbyid(r.id.pic_goods);
      itemview.id_goods = (textview) convertview
          .findviewbyid(r.id.id_goods);
      itemview.color_goods = (textview) convertview
          .findviewbyid(r.id.color_goods);
      itemview.type_goods = (textview) convertview
          .findviewbyid(r.id.type_goods);
      itemview.integral_goods = (textview) convertview
          .findviewbyid(r.id.integral_goods);
      itemview.number = (textview) convertview.findviewbyid(r.id.number);
      itemview.minus = (button) convertview.findviewbyid(r.id.minus);
      itemview.plus = (button) convertview.findviewbyid(r.id.plus);
      convertview.settag(itemview);
    } else {
      itemview = (viewholder) convertview.gettag();
    }

    init(itemview, position);

    itemview.ck_select.setchecked(getisselected().get(position));
    itemview.number.settext(getnumbers().get(position).tostring());
    if (getisselected().get(position)) {
      itemview.ck_select.setchecked(true);
    } else {
      itemview.ck_select.setchecked(false);
    }

    string a = itemview.number.gettext().tostring();
    loans.get(position).setnum(integer.valueof(a));

    test test = loans.get(position);
    itemview.id_goods.settext((charsequence) test.getid());
    itemview.color_goods.settext((charsequence) test.getcolor());
    itemview.type_goods.settext((charsequence) test.gettype());
    itemview.integral_goods.settext((charsequence) test.getintegral());
    itemview.pic_goods.setimageresource(r.drawable.shopping);
    return convertview;
  }

  private void init(final viewholder itemview, final int position) {

    itemview.ck_select
        .setoncheckedchangelistener(new oncheckedchangelistener() {

          @override
          public void oncheckedchanged(compoundbutton buttonview,
              boolean ischecked) {
            isselected.put(position, true);
            getisselected().put(position, ischecked);
            itemview.ck_select.setchecked(getisselected().get(
                position));
            handler.sendmessage(handler.obtainmessage(10,
                gettotalprice()));

            iterator iterator = isselected.entryset().iterator();
            list<boolean> array = new arraylist<boolean>();
            while (iterator.hasnext()) {
              hashmap.entry entry = (hashmap.entry) iterator
                  .next();
              integer key = (integer) entry.getkey();
              boolean val = (boolean) entry.getvalue();
              array.add(val);
            }
            handler.sendmessage(handler.obtainmessage(11, array));
          }
        });

    final string numstring = itemview.number.gettext().tostring();
    itemview.plus.setonclicklistener(new onclicklistener() {

      @override
      public void onclick(view v) {

        if (numstring == null || numstring.equals("")) {
          num = 1;
          itemview.number.settext("1");
        } else {
          if (++num < 1) // 先加,再判断
          {
            num--;

            toast.maketext(context, "请输入一个大于0的数字",
                toast.length_short).show();
          } else {
            itemview.number.settext(string.valueof(num));
            loans.get(position).setnum(num);
            numbers.put(position, num);
            handler.sendmessage(handler.obtainmessage(10,
                gettotalprice()));
            log.i("test", "+:" + num);
          }
        }
      }
    });
    itemview.minus.setonclicklistener(new onclicklistener() {

      @override
      public void onclick(view v) {

        if (numstring == null || numstring.equals("")) {
          num = 1;
          itemview.number.settext("1");
        } else {
          if (--num < 1) // 先加,再判断
          {
            num++;
            log.i("test", "-:" + num);
            toast.maketext(context, "请输入一个大于0的数字",
                toast.length_short).show();
            log.i("test", "-:" + num);
          } else {
            itemview.number.settext(string.valueof(num));
            log.i("test", "-:" + num);
            loans.get(position).setnum(num);
            numbers.put(position, num);
            handler.sendmessage(handler.obtainmessage(10,
                gettotalprice()));
          }
        }

      }
    });

  }

  /**
   * 计算选中商品的积分
   * 
   * @return 返回需要付费的总积分
   */
  private float gettotalprice() {
    test bean = null;
    float totalprice = 0;
    for (int i = 0; i < loans.size(); i++) {
      bean = loans.get(i);
      if (shoppingcartadapter.getisselected().get(i)) {
        totalprice += bean.getnum()
            * integer.valueof(bean.getintegral());
      }
    }
    return totalprice;
  }

  public static hashmap<integer, boolean> getisselected() {
    return isselected;
  }

  public static void setisselected(hashmap<integer, boolean> isselected) {
    shoppingcartadapter.isselected = isselected;
  }

  public static hashmap<integer, integer> getnumbers() {
    return numbers;
  }

  public static void setnumbers(hashmap<integer, integer> numbers) {
    shoppingcartadapter.numbers = numbers;
  }
}

        adapter中的xml代码:

        

[Java] 查看源文件 复制代码
<

        实体类:

        

[Java] 查看源文件 复制代码
package com.autoserve.net33.model;


public class test {
  @override
  public string tostring() {
    return "test [id=" + id + ", color=" + color
        + ", type=" + type + ", integral=" + integral + "]";
  }

  public string getid() {
    return id;
  }
  public void setid(string id) {
    this.id = id;
  }
  public string getcolor() {
    return color;
  }
  public void setcolor(string color) {
    this.color = color;
  }
  public string gettype() {
    return type;
  }
  public void settype(string type) {
    this.type = type;
  }
  public string getintegral() {
    return integral;
  }
  public void setintegral(string integral) {
    this.integral = integral;
  }
  private string id;
  private string color;
  private string type;
  private string integral;
  private int num;//商品数量
  private int sumintegral;
  private boolean ischoosed;   //商品是否在购物车中被选中

  public test(string id, string color, string type, string integral) {
    super();
    this.id = id;
    this.color = color;
    this.type = type;
    this.integral = integral;
  }
  public test() {
    super();
  }

  public int getnum() {
    return num;
  }

  public void setnum(int num) {
    this.num = num;
  }

  public int getsumintegral() {
    return sumintegral;
  }

  public void setsumintegral(int sumintegral) {
    this.sumintegral = sumintegral;
  }

  public boolean ischoosed() {
    return ischoosed;
  }

  public void setchoosed(boolean ischoosed) {
    this.ischoosed = ischoosed;
  }  
}



        

0

主题

9160

帖子

2412

安币

Android大神

Rank: 6Rank: 6

发表于 2018-1-24 00:19:34 | 显示全部楼层
楼主是好人,回个帖会有安币吗?

4

主题

9503

帖子

809

安币

代码手工艺人

Rank: 4

发表于 2018-1-24 18:21:25 | 显示全部楼层
感谢大神~

9

主题

9281

帖子

1827

安币

Android大神

Rank: 6Rank: 6

QQ达人

发表于 2018-1-25 15:06:44 | 显示全部楼层
感谢分享,安卓巴士有你更精彩:lol

85

主题

9633

帖子

2201

安币

Android大神

Rank: 6Rank: 6

发表于 2018-1-26 23:10:02 | 显示全部楼层
安卓巴士是个不错的网站,我来顶个贴~
发表于 2018-1-27 06:22:37 | 显示全部楼层
楼主威武,以后多发干货,多办活动~!

0

主题

5

帖子

260

安币

攻城狮

Rank: 3Rank: 3

发表于 2018-4-12 09:12:12 | 显示全部楼层
我只是路过打酱油的。

0

主题

8

帖子

25

安币

初级码农

Rank: 1

发表于 7 天前 | 显示全部楼层
能吧剩下的代码发我给我?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

站长推荐

通过邮件订阅最新安卓weekly信息
上一条 /4 下一条

下载安卓巴士客户端

全国最大的安卓开发者社区

联系我们
关闭
合作电话:
15618560077
Email:
805941275@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 下载客户端|申请友链|手机版|站点统计|安卓巴士 ( 粤ICP备15117877号 )

快速回复 返回顶部 返回列表