如何使用地图将数据插入我的 Firebase 数据库?

How to insert data using map into my Firebase database?

首先我是这样保存数据的form

Save.setOnClickListener(new View.OnClickListener() {
    @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD)
    @Override
    public void onClick(View v) {

        String refernce = TextRef.getEditText().getText().toString();
        String nompdv = textNomPdv.getEditText().getText().toString();
        String etat = textEtatCommande.getEditText().getText().toString();
        String datecommande = TextDateCommande.getEditText().getText().toString();
        String produitcommande = textProduit.getEditText().getText().toString();
        String qntcommande = editTextProduitQnt.getEditText().getText().toString();


        DatabaseReference newPost = reference.child(refernce);
        newPost.child("refernce").setValue(refernce);
        newPost.child("nompdv").setValue(nompdv);
        newPost.child("etat").setValue(etat);
        newPost.child("datecommande").setValue(datecommande);
        newPost.child("user_id").setValue(uid);
    
        DatabaseReference newOrder = reference.child(refernce).child("produit");
        newOrder.child(produitcommande).setValue(qntcommande);
    }
});

这是My database structure

我只想使用地图来保存“produit”(红色数据):“produitcommande”和“qntcommande”:

如果您只想更新 produit 子项中的值,可以通过以下方式完成:

Map<String, Object> values = new HashMap<>();
values.put("D3", "51");
values.put("D5L", "41");

DatabaseReference produitRef = reference.child(refernce).child("produit");
produitRef.setValue(values);

如果您想更新 produit 的某些属性,但不修改其他属性,您可以使用:

Map<String, Object> values = new HashMap<>();
values.put("D3", "51");
values.put("D6", "new value");

DatabaseReference produitRef = reference.child(refernce).child("produit");
produitRef.updateChildren(values);

这将 更新 D3,将添加一个新的 D6 属性,并保持 D5L 不变。