如何在android中将这种结构发送到firebase firestore?

时间:2018-01-14 13:15:49

标签: android firebase google-cloud-firestore

enter image description here

我正在使用这个结构

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();
            }

但它不起作用。它覆盖了最后保存的数据,但我想添加最新的对象。 Firestore中不允许使用ArrayList。

1 个答案:

答案 0 :(得分:1)

您正在寻找更新现有文档,因此请致电update。这要求您指定要更新的文档的哪些字段,例如newVendorName

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

有关详情,请阅读Firestore documentation on updating fields in nested objects