Reputation: 6971
The following way I can update fields in a document which I created from a HashMap:
public void saveNote(View v) {
String title = editTextTitle.getText().toString();
String description = editTextDescription.getText().toString();
Map<String, Object> note = new HashMap<>();
note.put(KEY_TITLE, title);
note.put(KEY_DESCRIPTION, description);
noteRef.set(note)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "Note saved", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Error!", Toast.LENGTH_SHORT).show();
Log.d(TAG, e.toString());
}
});
}
public void updateDescription(View v) {
String description = editTextDescription.getText().toString();
noteRef.update(KEY_DESCRIPTION, description);
}
How do I update fields when I save my document from a POJO and convert it back with toObject
? Any examples?
Upvotes: 4
Views: 1182
Reputation: 1138
To make it understand, ill explain with a simple pojo class.
this is the sample pojo class
User.java
public class User {
String name;
String mobile;
String email;
String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
I have one activity with single button and its code is as below
**Creating/adding new Document**
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseFirestore=FirebaseFirestore.getInstance();
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
User user=new User();
user.setName("ABCD");
user.setAddress("Some Address");
user.setEmail("some email");
user.setMobile("mobilenumber");
CollectionReference collectionReference=firebaseFirestore.collection("User");
collectionReference.document("UserOne").set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
{
Toast.makeText(this, "Document created/updated", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
Now watch the code inside click listener of button. Here am creating a User object and adding values to its field using setters.
After that am initializing CollectionReference
with the collection called User
and adding document to that collection by
collectionReference.document("UserOne").set(user).addOnCompleteListener()
Here the document is UserOne
and the object is user in set()
method. So this will add/create the document with name UserOne
inside User
collection.
Now How to update fields of existing document?
Method 1
This method will update entire document with the object value you pass in set()
method. You must know the documentId
of the document which you want to update.
You can simply change the value of field by creating new object like this. Here am changing the value of mobile
field.
User user=new User();
user.setName("ABCD");
user.setAddress("Some Address");
user.setEmail("some email");
user.setMobile("0123456789");
CollectionReference collectionReference=firebaseFirestore.collection("User");
collectionReference.document("UserOne").set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
{
Toast.makeText(this, "Document created/updated", Toast.LENGTH_SHORT).show();
}
}
});
}
});
now this will update the mobile
field by value 0123456789
Method 2
In this method you update particular field of your pojo class like this. Am updating mobile
field with value 55555
;
CollectionReference collectionReference=firebaseFirestore.collection("User");
collectionReference.document("UserOne").update("mobile", "55555").addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(this, "Document created/updated", Toast.LENGTH_SHORT).show();
}
}
});
Note: Here you must know the documentId
of document to update one of its fields
With this two method you must be able to update fields of your pojo class used in document. Hope this helps you.
Upvotes: 2