简体   繁体   中英

Unable to update profile values in firebase android java

This is the code I am using but I could not able to update values in firebase..

Database level is like:

users-> userID: fname,phone,country

I am trying to update phone number and name. I am using firebase authentication.

DatabaseReference reference;



 onCreate{ encloded in

 reference = FirebaseDatabase.getInstance().getReference("users").push();


 public void updateProfile(View view) {
    if (isNameChanged() || isPhoneChanged()) {
        //Toast.makeText(this, "Updated Successfully", Toast.LENGTH_SHORT).show();
    } else Toast.makeText(this, "No Change Observed", Toast.LENGTH_SHORT).show();
}

private boolean isPhoneChanged() {
    if (!pphone.equals(e_pphone.getText().toString())) {
        reference.child(userID).child("phone").setValue(e_pphone.getText().toString());
        pphone=e_pphone.getText().toString();
        return true;
    } else
        return false;

}
    private boolean isNameChanged() {
    if (!pname.equals(e_pname.getText().toString())) {

        final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        if(firebaseUser!=null) {

            reference.child(userID).child("fname").setValue(e_pname.getText().toString());
            pname = e_pname.getText().toString();
            Toast.makeText(this, userID, Toast.LENGTH_SHORT).show();
        }
        return true;
    } else {
        Toast.makeText(this, "NOOOO "+pname, Toast.LENGTH_SHORT).show();
        return false;
    }
}

Hy, as you're referencing new path every time ie

 FirebaseDatabase.getInstance().getReference("users").push();

here push() will generate new value not one which already exist in your database. as a result dataRef won't get a path and it won't update any information.

reference = FirebaseDatabase.getInstance().getReference("users").push();

instead of push() try with any static data first and then you'll get the idea where the actual problem is.

Finally retrieve the node value which you want to update that you'll be using after "users" in firebaseReference.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM