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

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

How to find sum of values in a recycler view

问题

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

以下是我的列表:

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

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

public void total(View view){
    int total_goods=0;
    for (int i=0;i<modelArrayList.size();i++){
        total_goods +=modelArrayList.get(i).getTotal_price();
    }
}

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

public void setCheckout_button(View view){
    Scanner scanner = new Scanner();
    scanner.ScanCode(CheckOutActivity.this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    final IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
    if (result !=null){
        if (result.getContents() !=null){
            addItemOptions.setVisibility(View.VISIBLE);
            code_input_checkout.setText(result.getContents());
        }
        else {
            Toast.makeText(this, "No results", Toast.LENGTH_LONG).show();
        }
    }
    else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

public void AddStock(View view){
    String code = String.valueOf(code_input_checkout.getText());
    String item = String.valueOf(name_input_checkout.getText());
    String quantity = String.valueOf(quantity_input_checkout.getText());
    String unit_Price = String.valueOf(unitprice_input_checkout.getText());
    String total = String.valueOf(totalprice_input_checkout.getText());
    RecyclerModel model = new RecyclerModel(code,item,quantity,unit_Price,total);
    modelArrayList.add(model);
    recyclerAdapter.notifyDataSetChanged();

    Toast.makeText(CheckOutActivity.this, "Data Added", Toast.LENGTH_SHORT).show();
    Scanner scanner = new Scanner();
    scanner.ScanCode(CheckOutActivity.this);
}
public void FinishAdding(View view){
    addItemOptions.setVisibility(View.GONE);
}
public void total(View view){
    int total_goods=0;
    for (int i=0;i<modelArrayList.size();i++) {
        total_goods = total_goods+modelArrayList.get(i);
    }
}

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

public class RecyclerModel {
    // 属性和方法

    public int getTotal_price() {
        return Integer.parseInt(total_price);
    }

    // 其他属性和方法
}
英文:

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

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

Below is my code from the receiving of the data

public void setCheckout_button(View view){
Scanner scanner = new Scanner();
scanner.ScanCode(CheckOutActivity.this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//    super.onActivityResult(requestCode, resultCode, data);
final IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
if (result !=null){
if (result.getContents() !=null){
addItemOptions.setVisibility(View.VISIBLE);
code_input_checkout.setText(result.getContents());
}
else {
Toast.makeText(this, &quot;No results&quot;, Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
public void AddStock(View view){
String code = String.valueOf(code_input_checkout.getText());
String item = String.valueOf(name_input_checkout.getText());
String quantity = String.valueOf(quantity_input_checkout.getText());
String unit_Price = String.valueOf(unitprice_input_checkout.getText());
String total = String.valueOf(totalprice_input_checkout.getText());
RecyclerModel model = new RecyclerModel(code,item,quantity,unit_Price,total);
modelArrayList.add(model);
recyclerAdapter.notifyDataSetChanged();
Toast.makeText(CheckOutActivity.this, &quot;Data Added&quot;, Toast.LENGTH_SHORT).show();
Scanner scanner = new Scanner();
scanner.ScanCode(CheckOutActivity.this);
}
public void FinishAdding(View view){
addItemOptions.setVisibility(View.GONE);
}
public void total(View view){
int total_goods=0;
for (int i=0;i&lt;modelArrayList.size();i++) {
total_goods = total_goods+modelArrayList.get(i);
}
}

My model class is below. The getTotal_price method also included

public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getUnit_price() {
return unit_price;
}
public void setUnit_price(String unit_price) {
this.unit_price = unit_price;
}
public String getTotal_price() {
return Integer.parseInt(total_price);
}
public void setTotal_price(String total_price) {
this.total_price = total_price;
}

}

答案1

得分: 1

replace your method total(); with code below:

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

replace your method total(); with code below:

int total_goods=0;
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

public int total(View view, ArrayList<Integer> modelArrayList){
    int total_goods=0;
    ArrayList<Integer> ma = modelArrayList;

    for (int i=0;i<ma.size();i++){
        total_goods = total_goods + ma.get(i);
       
    }
return total_goods;
}
英文:

Try

public int total(View view, ArrayList&lt;Integer&gt; modelArrayList){
int total_goods=0;
ArrayList&lt;Integer&gt; ma = modelArrayList;
for (int i=0;i&lt;ma.size();i++){
total_goods =total_goods + ma.get(i);
}
return total_goods;
}

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:

确定