Hello I'm trying make nested child node but I have no idea how
this is my data
I want make like fake bank
like under phone i want add another data called like donate and inside it I want add node called history like any time donor donate money it should store in the history anyone know how i can do that???
this is my codes
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donor__registration);
doname=(EditText)findViewById(R.id.editname);
dopass=(EditText)findViewById(R.id.editpass);
docon=(EditText)findViewById(R.id.editcon);
dophone=(EditText)findViewById(R.id.editphone);
doemail=(EditText)findViewById(R.id.editemail);
btn_register=(Button)findViewById(R.id.button2);
myref1= FirebaseDatabase.getInstance().getReference("Donors");
firebaseAuth =FirebaseAuth.getInstance();//d
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String do_name=doname.getText().toString();
String do_pass=dopass.getText().toString();
String do_con=docon.getText().toString();
String do_phone=dophone.getText().toString();
String do_email=doemail.getText().toString();
//validation
if(do_name.equals(""))
doname.setError("User name Should not be empty");
else if(do_pass.equals(""))
dopass.setError("Password Should not be empty");
else if(do_con.equals(""))
docon.setError("Confirm Password Should not be empty");
else if(do_phone.equals(""))
dophone.setError("Phone Should not be empty");
else if(do_email.equals(""))
doemail.setError("Email Should not be empty");
else if(!do_pass.equals(do_con))
docon.setError("Password and Confirm password should match");
else if(do_pass.length()<8)
dopass.setError("Password Should be at least 8 charaters");
else if(do_phone.length()<8)
dophone.setError("Phone number should be 8 digits");
else if(!do_phone.startsWith("7")&& !do_phone.startsWith("9"))
dophone.setError("Should Start with 7 or 9");
else if(!do_email.matches(emailpattern))
doemail.setError("Email Should be correct");
//else
else {
//
myref1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
int flag = 0;
for (DataSnapshot d : snapshot.getChildren()) {
Donors ado = d.getValue(Donors.class);
if (ado.d_email.equalsIgnoreCase(do_email)) {
flag = 1;
Toast.makeText(Donor_Registration.this, "Donor already exists", Toast.LENGTH_LONG).show();
}
}
if (flag == 0) {
//
firebaseAuth.createUserWithEmailAndPassword(doemail.getText().toString(),dopass.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(Donor_Registration.this,"Donor registered",Toast.LENGTH_LONG).show();
}
// else
// {
// Toast.makeText(Donor_Registration.this,task.getException().getMessage(),Toast.LENGTH_LONG).show();
// }
}
});
Donors ado = new Donors(do_name, do_pass, do_phone, do_email);
String id = myref1.push().getKey();
myref1.child(id).setValue(ado);
Toast.makeText(Donor_Registration.this, "Donor registered", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
});
}
Your code is setting the entire donor node.
FirebaseReference donorsNode = FirebaseDatabase.getInstance().getReference("Donors");
FirebaseReference donorNode = donorsNode.child(donorId);
donorNode.setValue(entireDonorValue);
To update it with a donation just set a child node inside the existing donor node.
FirebaseReference donorsNode = FirebaseDatabase.getInstance().getReference("Donors");
FirebaseReference donorNode = donorsNode.child(donorId);
FirebaseReference donationsNode = donorNode.child("Donations");
FirebaseReference donationNode = donationsNode.child(donationId);
donationNode.setValue(donationValue);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.