【问题标题】:How to insert data using map into my Firebase database?如何使用地图将数据插入我的 Firebase 数据库?
【发布时间】:2021-12-30 10:18:08
【问题描述】:

首先我以这种方式保存数据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”:

【问题讨论】:

    标签: java android firebase firebase-realtime-database android-recyclerview


    【解决方案1】:

    如果您只想更新 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 将保持不变。

    【讨论】:

    • 谢谢,我完成了添加这样的产品:``values.put(produitcommande, qntcommande); ``
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    相关资源
    最近更新 更多