我已设法使用以下代码检索火力基础数据。然而,我想对我刚刚提取的记录做更多的事情。具体而言我想要总和,因为它们是数值..
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);
}
所以有可能计算两个金额的总和? 我的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 :(得分:0)
在您的活动中将amount(int)声明为全局int变量,并将其初始化为零并替换 String amount =“Amount:”+ dogExpenditure.getAmount(); 同 量+ =量+ dogExpenditure.getAmount(); 如果这没有帮助,请通知我
答案 1 :(得分: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;
}
}
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
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。 这有帮助??