我已经尝试了许多方法来从孩子的所有孩子那里得到所有金额的总和,但是没有工作。
这是我完成该特定任务的主要活动:
public class hostelincome extends AppCompatActivity {
EditText name,amount,status,mod,seriesnumber;;
Button storedata;
TextView prevnumber;
FirebaseDatabase firebaseDatabase;
DatabaseReference reference,reference1;
String name1,amount1,status1,mod1,date,count1,count2,incometotal1;
double amount2,incometotal;
int total;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hostelincome);
name = (EditText)findViewById(R.id.name);
amount = (EditText)findViewById(R.id.amount);
status = (EditText)findViewById(R.id.status);
final String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
mod = (EditText)findViewById(R.id.mod);
storedata = (Button)findViewById(R.id.storedata);
firebaseDatabase = FirebaseDatabase.getInstance();
date = getIntent().getStringExtra("date");
reference =
firebaseDatabase.getReference("Hostel").child(date).child("Income").child(currentDateTimeString
);
storedata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name1 = name.getText().toString();
amount1 = amount.getText().toString();
status1 = status.getText().toString();
mod1 = mod.getText().toString();
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
total = 0;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String amount = ds.child("amount").getValue(String.class);
int value = Integer.valueOf(amount);
total =+ value;
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
count1 = Integer.toString(total);
Toast.makeText(getApplicationContext(),count1,Toast.LENGTH_LONG).show();
if (amount1.isEmpty()){}
else{
amount2 = Double.parseDouble(amount1);}
incomeinfo incomeinfo;
incomeinfo = new incomeinfo(name1,amount1,status1,mod1);
reference.setValue(incomeinfo);
}
});
}
}
日期被当作一个字符串,将日/月/年的整数值相加。
运行此程序时,我在添加以下值时出错:
Process: org.electromob.budget, PID: 21949
java.lang.NumberFormatException: s == null
at java.lang.Integer.parseInt(Integer.java:570)
at java.lang.Integer.valueOf(Integer.java:794)
at org.electromob.budget.hostelincome$1$1.onDataChange(hostelincome.java:71)
at
com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-
database@@19.2.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-
database@@19.2.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-
database@@19.2.0:55)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)'''
我的Firebase信息学习班
public class incomeinfo {
public String payeename;
public String amount;
public String status;
public String mod;
public incomeinfo() {
}
public incomeinfo(String payeename, String amount, String status, String mod) {
this.payeename = payeename;
this.amount = amount;
this.status = status;
this.mod = mod;
}
public String getPayeename() {
return payeename;
}
public void setPayeename(String payeename) {
this.payeename = payeename;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMod() {
return mod;
}
public void setMod(String mod) {
this.mod = mod;
}
}
Firebase结构添加到我的项目下面:
有人可以帮助我解决我的错误吗?
答案 0 :(得分:1)
您似乎在JSON中读取的级别太低。您的onDataChange
遍历各个amount
,mod
,payeename
和status
属性,然后这些属性不再有amount
子级。
如果确实如此,则可以通过将侦听器附加到更高一级来解决此问题:
reference = firebaseDatabase.getReference("Hostel").child(date).child("Income");