2017-03-07 54 views
0

我設法使用下面的代碼來獲取消防基礎數據的總和。然而,我願做更多的記錄,我只是pulled.To具體我想總和,因爲它們是數值..計算的數字火力記錄

public void onDataChange(DataSnapshot snapshot) { 
    for (DataSnapshot postSnapshot : snapshot.getChildren()) { 
     //Getting the data from snapshot 
     DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class); 

     //Adding it to a string 


     String amount = "Amount: "+dogExpenditure.getAmount(); 


     expenseList.add(expenses); 

      if (expenseList.size() == 1) { 
       adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, expenseList); 
       listview.setAdapter(adapter); 
      } else if (expenseList.size() > 1) { 

      } 
      System.out.println(expenses); 
      listview.setAdapter(adapter); 
     } 

我的數據庫看起來像這樣enter image description here

所以可能計算兩個數量的總和? 我DogExpenditure類是:

**public class DogExpenditure { 
    private String amount; 
    private String item; 

    public String getAmount() { 
     return amount; 
    } 

    public void setAmount(String amount) { 
     this.amount = amount; 
    } 

    public String getItem() { 
     return item; 
    } 

    public void setItem(String item) { 
     this.item = item; 
    } 
}** 
+0

哪裏是你DogExpenditure類? – xFighter

+0

我已經編輯我的問題@xFighter –

回答

0

申報量(INT)爲您的活動全局int變量,它intitalize零和替換 字符串量= 「金額」 + dogExpenditure.getAmount(); 與 量+ =量+ dogExpenditure.getAmount(); 通知我,如果這不利於

+0

我得到一個null1200015000 –

+0

其明確的形式輸出,同時上載量爲您上傳的其作爲字符串,而不是number.Hence而不是相加的數額正在concatinated.'0' 每個鍵也被轉換爲字符串null。 –

0

使用這個類來代替上述一個

公共類DogExpenditure {

private int amount; 
private String item; 

public int getAmount() { 
    return amount; 
} 
public String getItem() { 
    return item; 
} 

public void setItem(String item) { 
    this.item = item; 
} 

public void setAmount(int amount) { 
    this.amount = amount; 
} 

}

公共無效onDataChange(DataSnapshot快照){

for (DataSnapshot postSnapshot : snapshot.getChildren()) { 

    //Getting the data from snapshot 

    DogExpenditure dogExpenditure=postSnapshot.getValue(DogExpenditure.class); 

    //Adding it to a string 

    amount+=amount+dogExpenditure.getAmount(); 



    expenseList.add(expenses); 

     if (expenseList.size() == 1) { 
      adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, expenseList); 
      listview.setAdapter(adapter); 
     } else if (expenseList.size() > 1) { 

     } 
     System.out.println(expenses); 
     listview.setAdapter(adapter); 
    } 
public String getItem() { 
    return item; 
} 

public void setItem(String item) { 
    this.item = item; 
} 

} **

而且不要忘記活動開始時,雖然通過類上傳量值數據庫初始化量爲0,以確保它們是int類型。 這是幫助嗎?