从list recyclerview计算总价

nukf8bse  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(379)

我有一个嵌套列表,其中每个项目的列表都有一个pricereguler和一个priceafterdiscount。
如何计算总价调节器(如果priceafterdiscount==0)?所以结果是363000+158000=521000。
或如何计算总价(如果priceafterdiscount!=0)? 所以结果是这样的363000+119000=482000

@Override
public void onBindViewHolder(@NonNull AdapterHeaderCart.ViewHolder holder, int position) {
    ProductCountry productCountry = productCountryList.get(position);
    List<Product> productList = productCountry.getProductList();

    Log.e("TAG", "onBindViewHolder: " + productList.toString());
}
E/TAG: onBindViewHolder: [{totalAllProduct=0, finalPrice=0, totalPrice=0, image='https://google.com/171212847/file.jpg', images=null, kindId=0, kind=null, description='null', weight=0, source=Source{image = 'null',name = 'Shopciety',link = 'null',id = '0',slug = 'shopciety'}, usdPriceRegular=0.0, sourceLink='null', colors=null, priceRegular=363000, sizes=null, usdPriceAfterDiscount=0, name='4D CITYSCAPE Puzzle - Game of Thrones Winterfell 3D Puzzle', attributes=null, id=141922, sourceId=0, priceAfterDiscount=0, slug='null', isFeatured=false, country='USA'}]
E/TAG: onBindViewHolder: [{totalAllProduct=0, finalPrice=0, totalPrice=0, image='https://google.com/system/media_libraries/268171710/file.jpg', images=null, kindId=0, kind=null, description='null', weight=0, source=Source{image = 'null',name = 'Shopciety',link = 'null',id = '0',slug = 'shopciety'}, usdPriceRegular=0.0, sourceLink='null', colors=null, priceRegular=158000, sizes=null, usdPriceAfterDiscount=0, name='BIOGLAN Supplement - Gummies Probiotic 50 Chocolate Balls', attributes=null, id=142095, sourceId=0, priceAfterDiscount=119000, slug='null', isFeatured=false, country='Australia'}]
eagi6jfj

eagi6jfj1#

这个怎么样?

@Override
public void onBindViewHolder(@NonNull AdapterHeaderCart.ViewHolder holder, int position) {
    ProductCountry productCountry = productCountryList.get(position);
    List<Product> productList = productCountry.getProductList();

    Log.e("TAG", "onBindViewHolder: " + productList.toString());

    int sumPriceReguler = 0;
    int finalPosition = 0;
    if (productList.size() > 2) { //for get beforeItem
        for(int i = 0 ; i < productList.size() ; i ++) {
            Product product = productList.get(finalPosition);
            int beforeProductPrice = productList.get(position-1).getPriceReguler();
            if (product.getPriceAfterDiscount == 0) {
                sumPriceReguler = beforeProductPrice + product.getPriceReguler();
            } else {
                sumPriceReguler = beforeProductPrice + product.getPriceAfterDiscount()
            }
            finalPosition++;
        }
    }
    // use sumPriceReguler as you want
}

相关问题