繁体   English   中英

如何使用 hashmap 从数据库中检索数据?

[英]How to retrieve data from database using hashmap?

我有这个recyclerview ,当我单击一个项目时,我想从数据库中获取红色方块中的数据。

在此处输入图像描述

我想这样展示它

在此处输入图像描述

这就是我保存它的方式:

 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<String, Object> values = new HashMap<>();
            values.put(produitcommande, qntcommande);

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

这就是我从第一个活动(recyclerview)发送数据的方式:

v.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(view.getContext(), Order_detail.class);
            int pos = v.getAdapterPosition();
            if (pos != RecyclerView.NO_POSITION) {
                Order clickedDataItem = listArray.get(pos);

                intent.putExtra("nompdv", clickedDataItem.getNompdv());
                intent.putExtra("reference", clickedDataItem.getRefernce());
                intent.putExtra("datecommande", clickedDataItem.getDatecommande());
                intent.putExtra("etat", clickedDataItem.getEtat());
             }

            view.getContext().startActivity(intent);
        }
    });

单击recyclerview中的项目时如何获取产品及其数量?

更新:这是我的订单 class:

public class Order implements Serializable {
    public  String refernce, nompdv, etat, datecommande, produitcommande, qntcommande;
     String user_id;

    public Order(){}

    public Order(String refernce, String nompdv, String etat, String datecommande, String produitcommande, String qntcommande, String user_id) {
        this.refernce = refernce;
        this.nompdv = nompdv;
        this.etat = etat;
        this.datecommande = datecommande;
        this.produitcommande = produitcommande;
        this.qntcommande = qntcommande;
        this.user_id = user_id;

    }

    public String getRefernce() {
        return refernce;
    }

    public void setRefernce(String refernce) {
        this.refernce = refernce;
    }

    public String getNompdv() {
        return nompdv;
    }

    public void setNompdv(String nompdv) {
        this.nompdv = nompdv;
    }

    public String getEtat() {
        return etat;
    }

    public void setEtat(String etat) {
        this.etat = etat;
    }

    public String getDatecommande() {
        return datecommande;
    }

    public void setDatecommande(String datecommande) {
        this.datecommande = datecommande;
    }

    public String getProduitcommande() {
        return produitcommande;
    }

    public void setProduitcommande(String produitcommande) {
        this.produitcommande = produitcommande;
    }

    public String getQntcommande() {
        return qntcommande;
    }

    public void setQntcommande(String qntcommande) {
        this.qntcommande = qntcommande;
    }

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

}

我如何保存数据的表格

根据您的问题,我假设produit下的密钥是随机的。 因此,当您检索订单详细信息时, produit将是一个 LinkedHashmap。 class顺序将产品声明为 LinkedHashmap<String,String>。

您可以打印这些键和值。

for(Map.Entry<String, String> e : clickedDataItem.getproduit().entrySet()) {
      
      //  print  e.getKey() e.getValue()
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM