欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现二级购物车的全选加反选、总价功能

程序员文章站 2023-11-14 14:00:58
本文实例为大家分享了android实现二级购物车的全选加反选、总价的具体代码,供大家参考,具体内容如下 mainactivity import android....

本文实例为大家分享了android实现二级购物车的全选加反选、总价的具体代码,供大家参考,具体内容如下

mainactivity

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.support.v7.widget.linearlayoutmanager;
import android.support.v7.widget.recyclerview;
import android.util.log;
import android.view.view;
import android.widget.checkbox;
import android.widget.compoundbutton;
import android.widget.textview;

import java.util.arraylist;

import wangyaowei.bw.com.shopcart.adapter.shopadapter;
import wangyaowei.bw.com.shopcart.bean.databean;
import wangyaowei.bw.com.shopcart.bean.listbean;
import wangyaowei.bw.com.shopcart.bean.shopbean;
import wangyaowei.bw.com.shopcart.presenter.goodspresenter;
import wangyaowei.bw.com.shopcart.view.goodsview;

public class mainactivity extends appcompatactivity implements goodsview {

 private recyclerview rlv_shop;
 private goodspresenter goodspresenter;
 private checkbox box_all;
 private shopadapter shopadapter;
 private arraylist<databean> data;
 public textview sum;
 public int nums;
 public double prices;
 public double total;

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);

 sum = findviewbyid(r.id.sum);
 rlv_shop = findviewbyid(r.id.rlv_shop);
 linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(this);
 rlv_shop.setlayoutmanager(linearlayoutmanager);

 goodspresenter = new goodspresenter(this);
 goodspresenter.relaed();
 box_all = findviewbyid(r.id.box_all);

 }

 @override
 public void onresult(arraylist<databean> data) {
 this.data=data;
 shopadapter = new shopadapter(mainactivity.this, data);
 rlv_shop.setadapter(shopadapter);
 setaaa();

 }

 private void setaaa() {

 box_all.setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
  if(box_all.ischecked()){

   for(int i=0;i<data.size();i++){
   databean databean = data.get(i);
   databean.setflag(true);
   arraylist<listbean> list = databean.getlist();
   for (int j = 0; j < list.size(); j++) {
    listbean listbean = list.get(j);
    listbean.setflags(true);
   }
   }

  }
  else {
   for(int i=0;i<data.size();i++){
   databean databean = data.get(i);
   databean.setflag(false);
   arraylist<listbean> list = databean.getlist();
   for (int j = 0; j < list.size(); j++) {
    listbean listbean = list.get(j);
    listbean.setflags(false);
   }
   }
  }
  shopadapter.notifydatasetchanged();
  getprice();
  }
 });
 }

 private void getprice() {
 total = 0;
 int sums=0;
 for (int i = 0; i < data.size(); i++) {
  databean databean = data.get(i);
  arraylist<listbean> list = databean.getlist();
  for (int j = 0; j < list.size(); j++) {
  listbean listbean = list.get(j);
  if (listbean.isflags()){
   string num = listbean.getnum();
   nums = integer.valueof(num);
   string price =listbean.getprice();
   prices = double.valueof(price);
   total += nums * prices;
   sums++;
  }
  }

 }

 sum.settext(total +"");

 }
}

一级适配器

public class shopadapter extends recyclerview.adapter<shopadapter.myviewholder> {
 context context;
 arraylist<databean> beans;


 public shopadapter(context context, arraylist<databean> beans) {
 this.context = context;
 this.beans = beans;
 }

 @nonnull
 @override
 public myviewholder oncreateviewholder(@nonnull viewgroup viewgroup, int i) {
 view view=layoutinflater.from(context).inflate(r.layout.shop_layout,null,false);
 myviewholder myviewholder = new myviewholder(view);
 return myviewholder;
 }

 @override
 public void onbindviewholder(@nonnull myviewholder myviewholder, int i) {
 myviewholder.name_shop.settext(beans.get(i).getsellername());

 linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(context);
 myviewholder.rlv_goods.setlayoutmanager(linearlayoutmanager);

 arraylist<listbean> list = beans.get(i).getlist();
 goodsadapter goodsadapter = new goodsadapter(context, list);

 myviewholder.rlv_goods.setadapter(goodsadapter);
 myviewholder.box_shop.setchecked(beans.get(i).isflag());
 }

 @override
 public int getitemcount() {
 return beans.size();
 }

 public class myviewholder extends recyclerview.viewholder {

 private final textview name_shop;
 private final checkbox box_shop;
 private final recyclerview rlv_goods;
 public myviewholder(@nonnull view itemview) {
  super(itemview);
  name_shop = itemview.findviewbyid(r.id.name_shop);
  rlv_goods = itemview.findviewbyid(r.id.rlv_goods);
  box_shop = itemview.findviewbyid(r.id.box_shop);
 }
 }
}

二级适配器

public class goodsadapter extends recyclerview.adapter<goodsadapter.myviewholder> {
 context context;
 arraylist<listbean> beans;
 private int shu;

 public goodsadapter(context context, arraylist<listbean> beans) {
 this.context = context;
 this.beans = beans;
 }

 @nonnull
 @override
 public myviewholder oncreateviewholder(@nonnull viewgroup viewgroup, int i) {
 view view=layoutinflater.from(context).inflate(r.layout.goods_layout,null,false);
 myviewholder myviewholder = new myviewholder(view);
 return myviewholder;
 }

 @override
 public void onbindviewholder(@nonnull final myviewholder myviewholder, int i) {
 myviewholder.name_goods.settext(beans.get(i).gettitle());
 myviewholder.price_goods.settext(beans.get(i).getprice()+"");

 string num = beans.get(i).getnum();
 shu = integer.parseint(num);
 myviewholder.addview.nums.settext(shu +"");

 glide.with(context).load(beans.get(i).getimages()).into(myviewholder.img_goods);
 myviewholder.box_goods.setchecked(beans.get(i).isflags());

 myviewholder.addview.jia.setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
  shu++;
  myviewholder.addview.nums.settext(shu +"");
  }
 });
 myviewholder.addview.jian.setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
  if (shu>=1){
   shu--;
   myviewholder.addview.nums.settext(shu +"");
  }else {
   toast.maketext(context, "已经最少不能减了", toast.length_short).show();
  }
  }
 });

 }

 @override
 public int getitemcount() {
 return beans.size();
 }

 public class myviewholder extends recyclerview.viewholder {

 private final textview name_goods,price_goods;
 private final imageview img_goods;
 private final addview addview;
 private final checkbox box_goods;

 public myviewholder(@nonnull view itemview) {
  super(itemview);
  name_goods = itemview.findviewbyid(r.id.name_goods);
  price_goods = itemview.findviewbyid(r.id.price_goods);
  img_goods = itemview.findviewbyid(r.id.img_goods);
  addview = itemview.findviewbyid(r.id.addview);
  box_goods = itemview.findviewbyid(r.id.box_goods);
 }
 }
}

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