2

Is it possible to only update the data of a certain user on my android Firebase?How can I do it, if for example I'm using an admin account where can I update all user specifically?

For example on my firebase database enter image description here

I only want to update the user details of SCYQ--- I want to change the name,email,date. of the user only without affecting the information of other users?

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Yamiess
  • 251
  • 4
  • 15

2 Answers2

6

Yes it is possible:

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String useruid=user.getUid();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Accounts").child("user").child(useruid);
ref.child("date").setValue(newdate);
ref.child("email").setValue(newemail);
ref.child("name").setValue(newname);
ref.child("type").setValue(newtype);

If you have the list of names in a listview and you want to update them then, you can do the following:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String selectedFromList = (String) listview.getItemAtPosition(position);
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Accounts").child("user").child(useruid);
ref.orderByChild("name").equalTo(selectedFromList).addValueEventListener(new ValueEventListener(){
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {

  //update values here
     }
  }

   @Override
 public void onCancelled(FirebaseError firebaseError) {

            }
        });
    });
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
5

While Peter's answer works, it will send four separate writes to the database. This in turn will fire listeners to that user data four times, which may not be what you want.

To update parts of a node in one go, use the updateChildren() method.

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String useruid=user.getUid();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Accounts").child("user").child(useruid);
Map<String, Object> updates = new HashMap<String,Object>();

updates.put("date", newdate);
updates.put("email", newemail);
updates.put("name", newname);
updates.put("type", newtype);

ref.updateChildren(updates);

This now sends all four values in a single update command, all failing or succeeding together, and leading to one update to other clients.

Also see the section update specific fields in the Firebase documentation.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • answer makes a very good point here. Though Peter's answer is light, there is a huge advantage with Franks answer. That is: when working on a budget & loads of updates, you must realise, each setValue() will call your Cloud Functions Listener too, 4 times. That might be something you dont want as it would unnecessarily spike expenses. – Yo Apps Jun 20 '19 at 08:59