Android : 如何计算模型数组列表中的总数?
Android : How to calculate total in model array-list?
[抱歉语法错误] [请求编辑问题将不胜感激]
我想创建 totalHaveMoney
,我试图在 totalMoney
方法中循环数据 income - expense
,但是失败了。只能从一个数据中获取 income
,计算无效
型号:
public class MoneyManager {
String name, category;
String date, description, key;
Long income, expense, totalMoney;
public MoneyManager(String name, String category, String date, Long income,Long expense , String description) {
this.name = name;
this.category = category;
this.date = date;
this.income = income;
this.expense = expense;
this.description = description;
}
public MoneyManager() {
}
public Long totalMoney(){
Long haveMoney = getIncome();
for (int i = 0; i < this.getIncome(); i++){
haveMoney -= getExpense();
}
return haveMoney;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Long getExpense() {
return expense;
}
public void setExpense(Long expense) {
this.expense = expense;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Long getIncome() {
return income;
}
public void setIncome(Long income) {
this.income = income;
}
public Long getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(Long totalMoney) {
this.totalMoney = totalMoney;
}
}
当我获取数据时:
MoneyManager money = moneyManagers.get(position);
Resources resources = context.getResources();
Locale rupiah = new Locale("id", "id");
holder.textName.setText(String.valueOf(money.totalMoney()));
holder.textDate.setText(money.getDate());
money.totalMoney()
的结果与 income
相同,但未计算
任何帮助将不胜感激
如果您有 List
个,您可以遍历它们并添加
long sum = 0;
for (MoneyManager mm : listOfMoneyManager) { // or whatever your local variable is
sum += mm.getIncome() - mm.getExpense();
}
// after the iteration you have the total
System.out.println (sum);
[抱歉语法错误] [请求编辑问题将不胜感激]
我想创建 totalHaveMoney
,我试图在 totalMoney
方法中循环数据 income - expense
,但是失败了。只能从一个数据中获取 income
,计算无效
型号:
public class MoneyManager {
String name, category;
String date, description, key;
Long income, expense, totalMoney;
public MoneyManager(String name, String category, String date, Long income,Long expense , String description) {
this.name = name;
this.category = category;
this.date = date;
this.income = income;
this.expense = expense;
this.description = description;
}
public MoneyManager() {
}
public Long totalMoney(){
Long haveMoney = getIncome();
for (int i = 0; i < this.getIncome(); i++){
haveMoney -= getExpense();
}
return haveMoney;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Long getExpense() {
return expense;
}
public void setExpense(Long expense) {
this.expense = expense;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Long getIncome() {
return income;
}
public void setIncome(Long income) {
this.income = income;
}
public Long getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(Long totalMoney) {
this.totalMoney = totalMoney;
}
}
当我获取数据时:
MoneyManager money = moneyManagers.get(position);
Resources resources = context.getResources();
Locale rupiah = new Locale("id", "id");
holder.textName.setText(String.valueOf(money.totalMoney()));
holder.textDate.setText(money.getDate());
money.totalMoney()
的结果与 income
相同,但未计算
任何帮助将不胜感激
如果您有 List
个,您可以遍历它们并添加
long sum = 0;
for (MoneyManager mm : listOfMoneyManager) { // or whatever your local variable is
sum += mm.getIncome() - mm.getExpense();
}
// after the iteration you have the total
System.out.println (sum);