亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

計算recyclerview中物品的總價

計算recyclerview中物品的總價

呼啦一陣風 2021-06-25 18:08:23
我使用 volley 庫并從服務器加載 json 并將其顯示在 recyclerview 中。我想創建一個購物車并計算 recyclerview 項目的總價。我的問題是沒有計算物品的總成本。請幫幫我這是我的代碼:public class checkout extends AppCompatActivity {    String shop = SHOP;    List<Product6> productList6;    ProductsAdapter6 productsAdapter6;    RecyclerView recyclerView;    private ProductsAdapter6 adapter6;    private GridLayoutManager layoutManager;    RecyclerView.LayoutManager RecyclerViewLayoutManager;    private TextView subTotal;    private double mSubTotal = 0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_checkout);        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);        recyclerView.setHasFixedSize(true);        RecyclerViewLayoutManager = new LinearLayoutManager(getApplicationContext());        recyclerView.setLayoutManager(RecyclerViewLayoutManager);        adschild2();        productList6 = new ArrayList<>();        subTotal = (TextView) findViewById(R.id.sub_total);        mSubTotal = grandTotal(productList6);        subTotal.setText(String.valueOf(mSubTotal));    }    private int grandTotal(List<Product6> items) {        int totalPrice = 0;        for (int i = 0; i < items.size(); i++) {            totalPrice += items.get(i).getBack();        }        return totalPrice;    }    private void adschild2() {        StringRequest stringRequest = new StringRequest(Request.Method.GET, shop, new Response.Listener<String>() 
查看完整描述

2 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

您的來電

mSubTotal = grandTotal(productList6);

應該在你的呼吁中

adschild2()

在 onResponse - 解析 JSON 之后。

原因是 adschild2 是一個異步函數,它只有在 onResponse 被調用后才填充 productList6,而對 grandTotal 的調用是同步的,并計算空列表的總和。


查看完整回答
反對 回復 2021-06-30
  • 2 回答
  • 0 關注
  • 301 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號