How to make this kind of structure in android to be sent to firebase firestore?

enter image description here

I was using this structure

public class NewVendor
{
    public String title;


    public Map<String,String> array;

    public NewVendor(String title, Map<String, String> array) {
        this.title = title;
        this.array = array;
    }
}

Map<String,String> list = new HashMap<>();
        list.put("Vendor Name",newVendorName);
        list.put("Vendor Address",newVendorAddress);

        NewVendor newVendor = new NewVendor(newVendorName,list);

        db.collection("Bills").document(firebaseUser.getUid()).set(newVendor)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid)
                    {
                        Toast.makeText(AddNewBillActivity.this, "Success", Toast.LENGTH_SHORT).show();
                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(AddNewBillActivity.this, "Failure", Toast.LENGTH_SHORT).show();
            }

But it is not working.It is overriding the last saved data but I want to add the latest object. ArrayList is not permitted in Firestore.

1 answer

  • answered 2018-01-14 13:15 Frank van Puffelen

    You're looking to update an existing document, so should call update. This requires that you specify what fields of the document you want to update, in your case the newVendorName:

    Map<String,Object> updates = new HashMap<>();
    updates.put(newVendorName, newVendor);
            db.collection("Bills").document(firebaseUser.getUid()).update(updates)...
    

    For more on this, read the Firestore documentation on updating fields in nested objects.