Rustam Nugmanov
Rustam Nugmanov

Reputation: 33

How to change data in android firebase under created Uid?

I am going to change data in profile after a user is created using phone in firebase. I can't find a solution how to change data (add or change name and phone).

Here is the User.class

public class User {

    private String id;
    private String name;
    private String email;
    private String phone;

    public User() {
    }

    public User(String name, String email, String id, String phone) {
        this.name = name;
        this.email = email;
        this.phone = phone;
        this.id = id;
    }

    public User(String name, String email, String phone) {
        this.name = name;
        this.email = email;
        this.phone = phone;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

Here is the creation of User by phone auth:

usersDatabaseReference = FirebaseDatabase.getInstance().getReference().child( "users" );
mAuthPhone = bundle.getString( "PhoneNumber" ); // phone number

usersDatabaseReference.orderByChild("phone").equalTo(mAuthPhone)
                .addListenerForSingleValueEvent( new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.getValue() == null){
                            user = new User( );
                            user.setId(firebaseUser.getUid() );
                            user.setPhone(firebaseUser.getPhoneNumber());
                            usersDatabaseReference.push().setValue( user );
                        }
                    }


                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
    }

I try to change data after creation:


phoneMaskedEditText = view.findViewById( R.id.phoneMaskedEditText );
emailEditText = view.findViewById( R.id.emailEditText );
nameEditText = view.findViewById( R.id.nameEditText );
mAuth = FirebaseAuth.getInstance();

private void updateUser(final FirebaseUser firebaseUser) {

        usersDatabaseReference
                .addValueEventListener( new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                            user = dataSnapshot.getValue(User.class);
                            user.setPhone( phoneMaskedEditText.getText().toString() );
                            user.setName( nameEditText.getText().toString() );
                            user.setPhone( phoneMaskedEditText.getText().toString() );
                            usersDatabaseReference.child( mAuth.getCurrentUser().getUid() ).setValue( user );
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
    }

After updateUser starts there is a new data in firebase with a new uid equal to id of the current user. How to change data in the created data elements.

enter image description here

Upvotes: 0

Views: 98

Answers (1)

Chaitanya Chavali
Chaitanya Chavali

Reputation: 853

While adding a new user to database by phone auth, change

usersDatabaseReference.push().setValue( user );

to

usersDatabaseReference.child(firebaseUser.getUid()).setValue( user );

Also maybe you will have to remove your existing users and re-add them to see the effect.

Comment if you have any queries.

Upvotes: 1

Related Questions