So far we have learn about the how to add data in firebase and how to delete data from firebase , now we learn how to update values in firebase android. It is also similar to update the data just like in other database SQL.
Update data in firebase database android
Lets have an example to learn how to update data in firebase database android. Below is the snapshot of the firebase database. we will update the age to 29 ,name to rajesh and Profession to IT.
Before update screen shot
Java Code to update data in firbase database Android
updatechildren method is used to update the data in firebase and completion listeners is used which notify when data is updated and it is one of the important method and helps in keeping synchronization for further operations (CRUD).
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import java.util.HashMap; public class MainActivity extends AppCompatActivity { FirebaseDatabase db; DatabaseReference myRef; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //create cust object of HashMap type taking key as string and value as object HashMap<String,Object> cust1=new HashMap<>(); cust1.put("name","rajesh"); cust1.put("age","29"); cust1.put("profession","IT"); db=FirebaseDatabase.getInstance(); myRef=db.getReference("padhle"); //listener which tells about whether data is inserted or not. // update command and listeners are added myRef.updateChildren(cust1, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { Toast.makeText(MainActivity.this,"Inserted successfully",Toast.LENGTH_LONG).show(); } }); } }
Output :
After update screen shot
Note : The most common mistake we often do is using setValue method to update the data in firebase. Now lets take an example and see what happens when try to update the database with setValue method.
Update the data in firebase using setValue method
Before update screen shot
Java code to update the database using setValue methods
Here we are updating the same age to 29 ,name to rajesh and Profession to IT but this time we are using setValue method. Now have a look on the results.
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import java.util.HashMap; public class MainActivity extends AppCompatActivity { FirebaseDatabase db; DatabaseReference myRef; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //create cust object of HashMap type taking key as string and value as object HashMap<String,Object> cust1=new HashMap<>(); cust1.put("name","rajesh"); cust1.put("age","29"); cust1.put("profession","IT"); db=FirebaseDatabase.getInstance(); myRef=db.getReference("padhle"); //listener which tells about whether data is updated or not. // using setvalue method myRef.setValue(cust1, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { Toast.makeText(MainActivity.this,"Updated successfully",Toast.LENGTH_LONG).show(); } }); } }
Output : After setValue method executes
Salary and Sex child nodes get deleted and only the values age , name and profession over write. hence we lost the data. So make sure while using the setValue method. It is not appropriate method to update the data in firebase using objects.
Now we have clear understanding of how to update the data in firebase now , but what we do when we have multiple values in child node and we have to update any specific entry like we have 50 customers data and we want to update the data of specific customer so let’s learn how to achieve updating data when having multiple values in child node of firebase android.
Update data having multiple values in firebase android
Before screen shot of firebase database
We are having 2 keys in child node padhle , now we will try to update the age to “40 ” on the basis of email id ( email id is primary key for this child node ).
Java code to update the data in firebase having multiple values
here we are first searching the key of the node where “rajesh@gmail.com” is present and then update the age of the rajesh.
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; import java.util.HashMap; public class MainActivity extends AppCompatActivity { FirebaseDatabase db; DatabaseReference myRef; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); db=FirebaseDatabase.getInstance(); myRef=db.getReference("padhle"); myRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot ds:dataSnapshot.getChildren()) { for(DataSnapshot dsw:ds.getChildren()) { if(dsw.getKey().contentEquals("email")) { if(dsw.getValue().toString().contentEquals("rajesh@gmail.com")) { myRef.child(ds.getKey()).child("age").setValue("40"); } } } } } @Override public void onCancelled(DatabaseError databaseError) { } }); } }
Output : Rajesh age is updated to “40”.
so this is all about how to update data in firebase android , if you have any query or any doubt please leave leave in below comments.