[英]How to show the value after push Firebase android?
I am currently working on my final project, i got confused about how to show the value (expenses) from my firebase because I am using push, 我目前正在完成自己的最终项目,由于如何使用push,我对如何显示Firebase的价值(费用)感到困惑,
So this is my database 这是我的数据库
and here is my code to add it on database 这是我的代码将其添加到数据库
DatabaseReference itemsRef = mRef.child("Users/" +
mAuth.getCurrentUser().getUid()).push();
itemsRef.child("expenses").setValue(total);
EditText itemedit = findViewById(R.id.macam1);
macam1 = itemedit.getText().toString();
itemsRef.child("items/").setValue(macam1 + ": " + item01);
EditText itemedit2 = findViewById(R.id.macam2);
macam2 = itemedit2.getText().toString();
itemsRef.child("items2/").setValue(macam2 + ": " + item02);
my User.class 我的User.class
public class User {
private String name;
private int balance,expenses;
public User(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public void setExpenses(int expenses) {
this.expenses = expenses;
}
public int getExpenses() {
return expenses;
}
}
this is the code so far: 这是到目前为止的代码:
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference("Users");
final String key = mRef.push().getKey();
String userid=user.getUid();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users").child(userid).child(key);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int expense = (int) dataSnapshot.child("expenses").getValue();
TextView view = (TextView) findViewById(R.id.totalExpense);
view.setText("" + expense);
}
@Override
public void onCancelled(DatabaseError error) {
}
});
the ref
already gave the value of the randomid but still error on int expense = (int) dataSnapshot.child("expenses").getValue();
ref
已经给出了randomid的值,但在int expense = (int) dataSnapshot.child("expenses").getValue();
上仍然出错int expense = (int) dataSnapshot.child("expenses").getValue();
To retrieve expenses from the database, try the following: 要从数据库检索费用,请尝试以下操作:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users").child(userid);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String expenses=datas.child("expenses").getValue().toString();
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
In your database, you have Users/userid/randomid/key:value
. 在数据库中,您具有Users/userid/randomid/key:value
。 So let the datasnapshot be at the userid
then loop inside the randomid to be able to retrieve the child expenses
因此,让数据快照位于userid
处,然后在randomid内循环以能够检索子项expenses
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.