You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Map向Firebase数据库插入produit对应字段数据?

实现代码

修改produit节点写入逻辑后的完整代码如下:

import java.util.HashMap;
import java.util.Map;

// 原有其他代码保持不变
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);

        // 用Map存储produit相关的产品名、数量键值对
        Map<String, Object> produitMap = new HashMap<>();
        produitMap.put(produitcommande, qntcommande);
        newPost.child("produit").setValue(produitMap);
    }
});

说明

  • 最终生成的数据库结构和原有逐节点写入的效果完全一致,仅修改了produit节点的写入逻辑符合Map存储的要求
  • 如果需要优化写入性能,可将所有订单字段都放入同一个Map对象,一次性写入对应节点,减少和Firebase数据库的交互次数

内容的提问来源于stack exchange,提问作者Mohamed Yacine Zouaoui

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 19:06:01