Home > Net >  How to insert data using map into my Firebase database?
How to insert data using map into my Firebase database?

Time:11-20

First I am saving data in this way 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);
    }
});

This is My database structure

I want to use map only for saving "produit" (data in red): "produitcommande" and "qntcommande":

this

CodePudding user response:

If you want to only update the values in the produit child, that can be done with:

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

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

If you want to update some properties of produit, but leave the others unmodified, you can do that with:

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

This will update D3, will add a new D6 property, and will leave D5L unmodified.

  • Related