Good I'm doing a job in firebase with android and I would like to know what the code would be like to edit a user's data; that is, the user enters with his account, sees his data and presses a button that says to edit data, be it his name, photo, password, etc. At the moment a user registers he does so with his mail and password and also enters his name, cell phone, and that data is stored in the firebase database and the mail and password in the user authentication of firebase, the part to show the user data I already did, I just want to know how I do so that user can edit their data. Thanks
Here the user and password are saved
Here the data of the registered user is saved
This example shows the name and photo associated with a user's account, as well as modifying the user's photo, but not how to update the user's name with a new one
public class Main3Activity extends AppCompatActivity {
private TextView txtusu;
private Button btnatras;
private ImageView imgperfil;
private ProgressDialog progressDialog;
private DatabaseReference mDataBase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private int CAMERA_REQUEST_CODE = 1;
private StorageReference mStorage;
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mAuth = FirebaseAuth.getInstance();
progressDialog= new ProgressDialog(this);
txtusu= (TextView) findViewById(R.id.txtusu);
btnatras= (Button) findViewById(R.id.btnatras);
imgperfil= (ImageView) findViewById(R.id.imgperfil);
mStorage=FirebaseStorage.getInstance().getReference();
imgperfil.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
if (intent.resolveActivity(getPackageManager()) != null){
startActivityForResult(Intent.createChooser(intent, "Seleccione una imagen de la galería"), CAMERA_REQUEST_CODE);
}
}
});
btnatras = (Button) findViewById(R.id.btnatras);
btnatras.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mAuth.getCurrentUser() != null)
mAuth.signOut();
}
});
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null){
mStorage= FirebaseStorage.getInstance().getReference();
mDataBase = FirebaseDatabase.getInstance().getReference().child("Alumnos");
mDataBase.child(firebaseAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
txtusu.setText(String.valueOf(dataSnapshot.child("Usuario").getValue()));
String imageUrl=String.valueOf(dataSnapshot.child("Foto_Perfil").getValue());
if (URLUtil.isValidUrl(imageUrl)){
Picasso.with(Main3Activity.this).load(Uri.parse(imageUrl)).into(imgperfil);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}else {
startActivity(new Intent(Main3Activity.this, Main2Activity.class));
finish();
}
}
};
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
if (mAuth.getCurrentUser() == null) {
return;
}
progressDialog.setMessage("Cargando imagen...");
progressDialog.show();
final Uri uri = data.getData();
if (uri == null) {
progressDialog.dismiss();
return;
}
if (mAuth.getCurrentUser() == null)
return;
if (mStorage == null)
mStorage = FirebaseStorage.getInstance().getReference();
if (mDataBase == null)
mDataBase = FirebaseDatabase.getInstance().getReference().child("Alumnos");
final StorageReference filepath = mStorage.child("Foto_Perfil").child(uri.getLastPathSegment());
final DatabaseReference currentUserDB = mDataBase.child(mAuth.getCurrentUser().getUid());
currentUserDB.child("Foto_Perfil").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String image = dataSnapshot.getValue().toString();
if (!image.equals("default") && !image.isEmpty()) {
Task<Void> task = FirebaseStorage.getInstance().getReferenceFromUrl(image).delete();
task.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(Main3Activity.this, "Imagen borrada", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Main3Activity.this, "Error, Imagen no borrada", Toast.LENGTH_SHORT).show();
}
}
});
}
currentUserDB.child("Foto_Perfil").removeEventListener(this);
filepath.putFile(uri).addOnSuccessListener(Main3Activity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Uri downloadUri = taskSnapshot.getDownloadUrl();
Toast.makeText(Main3Activity.this, "Finalizado", Toast.LENGTH_SHORT).show();
Picasso.with(Main3Activity.this).load(downloadUri).fit().centerCrop().into(imgperfil);
final DatabaseReference currentUserDB = mDataBase.child(mAuth.getCurrentUser().getUid());
currentUserDB.child("Foto_Perfil").setValue(downloadUri.toString());
}
}).addOnFailureListener(Main3Activity.this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(Main3Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
}