如何在 RecyclerView 中找到数值的总和。

huangapple go评论102阅读模式
英文:

How to find sum of values in a recycler view

问题

我有一个包含一些价格的列表在一个回收视图中。我想要计算总价格。我该如何做呢?

以下是我的列表:

如何在 RecyclerView 中找到数值的总和。

以下是我拥有的函数,但对我不起作用:

  1. public void total(View view){
  2. int total_goods=0;
  3. for (int i=0;i<modelArrayList.size();i++){
  4. total_goods +=modelArrayList.get(i).getTotal_price();
  5. }
  6. }

以下是我从接收数据的代码:

  1. public void setCheckout_button(View view){
  2. Scanner scanner = new Scanner();
  3. scanner.ScanCode(CheckOutActivity.this);
  4. }
  5. @Override
  6. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  7. final IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
  8. if (result !=null){
  9. if (result.getContents() !=null){
  10. addItemOptions.setVisibility(View.VISIBLE);
  11. code_input_checkout.setText(result.getContents());
  12. }
  13. else {
  14. Toast.makeText(this, "No results", Toast.LENGTH_LONG).show();
  15. }
  16. }
  17. else {
  18. super.onActivityResult(requestCode, resultCode, data);
  19. }
  20. }
  21. public void AddStock(View view){
  22. String code = String.valueOf(code_input_checkout.getText());
  23. String item = String.valueOf(name_input_checkout.getText());
  24. String quantity = String.valueOf(quantity_input_checkout.getText());
  25. String unit_Price = String.valueOf(unitprice_input_checkout.getText());
  26. String total = String.valueOf(totalprice_input_checkout.getText());
  27. RecyclerModel model = new RecyclerModel(code,item,quantity,unit_Price,total);
  28. modelArrayList.add(model);
  29. recyclerAdapter.notifyDataSetChanged();
  30. Toast.makeText(CheckOutActivity.this, "Data Added", Toast.LENGTH_SHORT).show();
  31. Scanner scanner = new Scanner();
  32. scanner.ScanCode(CheckOutActivity.this);
  33. }
  34. public void FinishAdding(View view){
  35. addItemOptions.setVisibility(View.GONE);
  36. }
  37. public void total(View view){
  38. int total_goods=0;
  39. for (int i=0;i<modelArrayList.size();i++) {
  40. total_goods = total_goods+modelArrayList.get(i);
  41. }
  42. }

我的模型类如下。也包括了 getTotal_price 方法:

  1. public class RecyclerModel {
  2. // 属性和方法
  3. public int getTotal_price() {
  4. return Integer.parseInt(total_price);
  5. }
  6. // 其他属性和方法
  7. }
英文:

I have a list containing some prices in a recycler view. I would like to find the total prices. How would I do it?
below is my list

如何在 RecyclerView 中找到数值的总和。

Below is the function that I have but it is not working for me

  1. public void total(View view){
  2. int total_goods=0;
  3. for (int i=0;i&lt;modelArrayList.size();i++){
  4. total_goods +=modelArrayList.get(i).getTotal_price();
  5. }
  6. }

Below is my code from the receiving of the data

  1. public void setCheckout_button(View view){
  2. Scanner scanner = new Scanner();
  3. scanner.ScanCode(CheckOutActivity.this);
  4. }
  5. @Override
  6. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  7. // super.onActivityResult(requestCode, resultCode, data);
  8. final IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
  9. if (result !=null){
  10. if (result.getContents() !=null){
  11. addItemOptions.setVisibility(View.VISIBLE);
  12. code_input_checkout.setText(result.getContents());
  13. }
  14. else {
  15. Toast.makeText(this, &quot;No results&quot;, Toast.LENGTH_LONG).show();
  16. }
  17. }
  18. else {
  19. super.onActivityResult(requestCode, resultCode, data);
  20. }
  21. }
  22. public void AddStock(View view){
  23. String code = String.valueOf(code_input_checkout.getText());
  24. String item = String.valueOf(name_input_checkout.getText());
  25. String quantity = String.valueOf(quantity_input_checkout.getText());
  26. String unit_Price = String.valueOf(unitprice_input_checkout.getText());
  27. String total = String.valueOf(totalprice_input_checkout.getText());
  28. RecyclerModel model = new RecyclerModel(code,item,quantity,unit_Price,total);
  29. modelArrayList.add(model);
  30. recyclerAdapter.notifyDataSetChanged();
  31. Toast.makeText(CheckOutActivity.this, &quot;Data Added&quot;, Toast.LENGTH_SHORT).show();
  32. Scanner scanner = new Scanner();
  33. scanner.ScanCode(CheckOutActivity.this);
  34. }
  35. public void FinishAdding(View view){
  36. addItemOptions.setVisibility(View.GONE);
  37. }
  38. public void total(View view){
  39. int total_goods=0;
  40. for (int i=0;i&lt;modelArrayList.size();i++) {
  41. total_goods = total_goods+modelArrayList.get(i);
  42. }
  43. }

My model class is below. The getTotal_price method also included

  1. public String getCode() {
  2. return code;
  3. }
  4. public void setCode(String code) {
  5. this.code = code;
  6. }
  7. public String getItem() {
  8. return item;
  9. }
  10. public void setItem(String item) {
  11. this.item = item;
  12. }
  13. public String getQuantity() {
  14. return quantity;
  15. }
  16. public void setQuantity(String quantity) {
  17. this.quantity = quantity;
  18. }
  19. public String getUnit_price() {
  20. return unit_price;
  21. }
  22. public void setUnit_price(String unit_price) {
  23. this.unit_price = unit_price;
  24. }
  25. public String getTotal_price() {
  26. return Integer.parseInt(total_price);
  27. }
  28. public void setTotal_price(String total_price) {
  29. this.total_price = total_price;
  30. }

}

答案1

得分: 1

replace your method total(); with code below:

  1. int total_goods=0;
  2. for (int i = 0; i < modelArrayList.size(); i++) total_goods+= modelArrayList.get(i).getTotal_price();
英文:

replace your method total(); with code below:

  1. int total_goods=0;
  2. for (int i = 0; i &lt; modelArrayList.size(); i++) total_goods+= modelArrayList.get(i).getTotal_price();

答案2

得分: 0

你何时调用了 total() 函数?是在 modelArrayList 接收数据之前还是之后?

英文:

When did you call total() function? Before or after modelArrayList receive data?

答案3

得分: 0

  1. public int total(View view, ArrayList<Integer> modelArrayList){
  2. int total_goods=0;
  3. ArrayList<Integer> ma = modelArrayList;
  4. for (int i=0;i<ma.size();i++){
  5. total_goods = total_goods + ma.get(i);
  6. }
  7. return total_goods;
  8. }
英文:

Try

  1. public int total(View view, ArrayList&lt;Integer&gt; modelArrayList){
  2. int total_goods=0;
  3. ArrayList&lt;Integer&gt; ma = modelArrayList;
  4. for (int i=0;i&lt;ma.size();i++){
  5. total_goods =total_goods + ma.get(i);
  6. }
  7. return total_goods;
  8. }

huangapple
  • 本文由 发表于 2020年9月28日 17:09:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64099161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定