使用for循环遍历项目列表

时间:2019-07-04 12:18:23

标签: java android list for-loop

我尝试遍历列表,但只获取列表的最后一个条目。我想遍历列表,将列表中的每个“利润”项目添加到current_profit中,最后能够返回应计current_profit。方法getFinanceList()返回大小为2的列表,如图所示。

这是我的调试日志。它显示了代码中我的方法返回的大小为2的列表。使用getFinanceList()

 ((FinanceRepository)this).financeList = {ArrayList@6012}  size = 2
 this = {FinanceRepository@6004} 
 current_expenditure = 0
 current_income = 0
 current_profit = -1000
 farmerApp = {BoxStore@6010} 
 financeBox = {Box@6011} 
 financeList = {ArrayList@6012}  size = 2
0 = {Finance@6025} 
 __boxStore = {BoxStore@6010} 
 category = "farm"
 finance_type = "Income"
 id = 1
 name = "jembe"
 notes = "hdgsbsbsb.."
 payment_type = "Cash"
 profit = {Integer@6032} 500  //this profit
 quantity = "5"
 total_amount = "500"
 total_expenditure = {Integer@6035} 0
 total_income = {Integer@6036} 500
 transaction_date = "17-7-2019"
 user = {ToOne@6038} 
 mCallbacks = null
 shadow$_klass_ = {Class@4418} "class 
com.example.e_farmer.models.Finance"
 shadow$_monitor_ = -2069821990
1 = {Finance@6026} 
 __boxStore = {BoxStore@6010} 
 category = "farm"
 finance_type = "Expenditure"
 id = 2
 name = "spade"
 notes = "bdhxhdd"
 payment_type = "Cash"
 profit = {Integer@6045} -1000 //And this profit
 quantity = "5"
 total_amount = "1000"
 total_expenditure = {Integer@6048} 1000
 total_income = {Integer@6049} 0
 transaction_date = "18-7-2019"
 user = {ToOne@6051} 
 mCallbacks = null
 shadow$_klass_ = {Class@4418} "class 
com.example.e_farmer.models.Finance"
 shadow$_monitor_ = -1997268469
 shadow$_klass_ = {Class@5986} "class 
com.example.e_farmer.repositories.FinanceRepository"
 shadow$_monitor_ = -2070028009
 current_profit = -1000

这就是我的代码。请注意,财务课是我的模型课

   public MutableLiveData<Integer> getProfit() {

   //the method getFinanceList returns a financeList of size 2 as  illustrated above in the debug code.

    getFinanceList();

    int current_profit = 0;

    for (int i = 0; i < financeList.size(); i++) {
        Finance finance = financeList.get(i);
        current_profit =+finance.getProfit();
    }

    Log.d(TAG, "getProfit: " + current_profit);

    MutableLiveData<Integer> profit_data = new MutableLiveData<>();
    profit_data.setValue(current_profit);

    return profit_data;
}

我期望current_profit获得第一项和第二项的利润,并且总计为500 +(-1000)=-500,但仅获得最后一项(-1000)。

2 个答案:

答案 0 :(得分:2)

您写current_profit =+finance.getProfit()的意思是current_profit += finance.getProfit()。您只需分配值,并在其上调用一元+,就不会执行任何操作。将值添加到前一个值的运算符是+=

答案 1 :(得分:1)

更正后的方法,问题是= +,变成了+ =

public MutableLiveData<Integer> getProfit() {

   //the method getFinanceList returns a financeList of size 2 as  illustrated above in the debug code.

    getFinanceList();

    int current_profit = 0;

    for (int i = 0; i < financeList.size(); i++) {
        Finance finance = financeList.get(i);
        current_profit +=finance.getProfit();
    }

    Log.d(TAG, "getProfit: " + current_profit);

    MutableLiveData<Integer> profit_data = new MutableLiveData<>();
    profit_data.setValue(current_profit);

    return profit_data;
}