我在计算收入的SUM
时遇到问题
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String transpo = String.valueOf(dataSnapshot.child("Income").getValue());
//int amount = Integer.parseInt(String.valueOf(dataSnapshot.child("amount").getValue()));
Map<String, String> amount = (Map<String, String>)dataSnapshot.child("amount").getValue();
//该模型称为收入模型
GenericTypeIndicator<HashMap<String, Income>> t = new
GenericTypeIndicator<HashMap<String, Income>>() {};
HashMap<String, Income> expenses = (HashMap<String, Income>) dataSnapshot.child("Income").getValue();
//遍历它以获得总和
double sum = 0;
for(HashMap.Entry<String, Income> entry : expenses.entrySet())
{
sum += entry.getValue().getIncome();
}
//现在有你的总和
Log.d(TAG,"Sum= " + sum);
txt_tzt.setText(transpo);
txtIncome.setText(String.valueOf(sum));
}
这是我的模特
package Model;
public class Income {
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public Integer getIncome() {
return Income;
}
public void setIncome(Integer income) {
Income = income;
}
public String getOrganisations() {
return Organisations;
}
public void setOrganisations(String organisations) {
Organisations = organisations;
}
public String getAccount() {
return Account;
}
public void setAccount(String account) {
Account = account;
}
public String getFirekey() {
return firekey;
}
public void setFirekey(String firekey) {
this.firekey = firekey;
}
//我希望收入总额将值传递给hashmap循环并添加新值
private Integer Income;
private String Description;
private String Organisations;
private String Account;
private String firekey;
public Income(Integer Income,String Description,String Organisations,String Account,String firekey){
this.Income=Income;
this.Description=Description;
this.Organisations=Organisations;
this.Account=Account;
this.firekey=firekey;
}
}
答案 0 :(得分:0)
使用Value事件侦听器
addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
int transpo = 0;
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
dreff.child("income");
Income income = postSnapshot.getValue(Income.class);
transpo += income.getIncome();
}
让我知道Macharia是怎么回事