- For Kotlin
private lateinit var database:DatabaseReference
database=FirebaseDatabase.getInstance().getReference("User") //Here "User" is database reference
val User=User(name,email,empid, number) //Here User is Class name or kotlin data file where these fields have taken
database.child(empid).setValue(User).addOnSuccessListener { //empid just a reference to show title of data
Toast.makeText(applicationContext,"Success",Toast.LENGTH_SHORT)
name= binding.name.text.clear().toString()
email=binding.email.text.clear().toString()
number=binding.num.text.clear().toString()
empid=binding.empid.text.clear().toString()
}
.addOnFailureListener{
Toast.makeText(this,"Failure",Toast.LENGTH_SHORT)
}}
}
- For Java
DatabaseReference databaseReference;
databaseReference= FirebaseDatabase.getInstance().getReference("User"); //Here "User" is database reference
DataJava dataJava=new DataJava(name,email,empid,number); //Here User is Class name or kotlin data file where these fields have taken
databaseReference.child(name).setValue(dataJava).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(JavaFirebaseActivity.this, "Success", Toast.LENGTH_SHORT).show();
jbinding.name1.getText().clear();
jbinding.email1.getText().clear();
jbinding.empid1.getText().clear();
jbinding.num1.getText().clear();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(JavaFirebaseActivity.this, "Failure", Toast.LENGTH_SHORT).show();
}
});
0 Comments