我在這裏有一個像這樣的數據庫結構。計算Firebase記錄值的總和
我的目標是獲得總兩項金額12000和15000的,這將是27000..However我曾嘗試使用是給我一個價值像null1200015000的代碼。這裏是代碼:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mystatement);
listview = (ListView) findViewById(R.id.listview);
textView4 = (TextView) findViewById(R.id.textView4);
dref = FirebaseDatabase.getInstance().getReference();
DatabaseReference dref = FirebaseDatabase.getInstance().getReference();
dref = dref.child("Expenditure");
dref.addValueEventListener(new ValueEventListener() {
@Override
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 stringString expenses = "Amount: "+dogExpenditure.getAmount()+"\nReason for Use: "+dogExpenditure.getItem()+"\n\n";
String amount = dogExpenditure.getAmount();
textView4.setText(amount);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
任何人有關於如何實現這一點的想法? 這裏是我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;
}
}
不能更改Amount Data類型爲int嗎?這樣做更有意義,你可以避免字符串連接 – IsuruKusumal