Home > Mobile >  How to retrieve data from database using hashmap?
How to retrieve data from database using hashmap?

Time:11-22

I have this enter image description here

And I want to show it like that

enter image description here

This is how I save it :

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

This is how I send data from first activity (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);
        }
    });

How to do to get the products and their quantities when clicking on an item in recyclerview?

Update : This my Order 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;
    }

}

The form how I save data

Update 2 :

This the Adapter for the recyclerview :

public class CommandeAdapter extends 
RecyclerView.Adapter<CommandeAdapter.CommandeViewHolder> {
List<Order> listArray;

public CommandeAdapter(List<Order> List) {
    this.listArray = List;
}

@NonNull
@Override
public CommandeAdapter.CommandeViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.commande_model, parent, false);

    return new CommandeViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull @NotNull CommandeAdapter.CommandeViewHolder holder, int position) {

    CommandeViewHolder v = (CommandeViewHolder) holder;
    final Order dataa = listArray.get(position);
    v.nom_pdv.setText(dataa.getNompdv());
    v.date.setText(dataa.getDatecommande());
    v.etat.setText(dataa.getEtat());

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

public class CommandeViewHolder extends RecyclerView.ViewHolder {
    TextView nom_pdv, date, etat;

    public CommandeViewHolder(@NonNull @NotNull View itemView) {
        super(itemView);

        nom_pdv = itemView.findViewById(R.id.namepdv);
        date = itemView.findViewById(R.id.dateliv);
        etat = itemView.findViewById(R.id.etat);
     }
}

@Override
public int getItemCount() {
    return listArray.size();
}}

And this is activity detail :

public class Order_detail extends AppCompatActivity {

TextView namepdv, refcommande, datecommande, etat, produitcmnd, qnt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_commande_detail2);

    namepdv = findViewById(R.id.nompdv);
    refcommande = findViewById(R.id.refcommande);
    datecommande = findViewById(R.id.datecommande);
    etat = findViewById(R.id.etatcommande);
     //  --------------------- //
    namepdv.setText(getIntent().getStringExtra("nompdv"));
    refcommande.setText(getIntent().getStringExtra("reference"));
    datecommande.setText(getIntent().getStringExtra("datecommande"));
    etat.setText(getIntent().getStringExtra("etat"));
 }}

CodePudding user response:

From your question I am assuming that keys under produit are random. So when you will retrieve order details produit will be a Hashmap. Declare produit as Hashmap<String,String> in Order class.

You can print those keys and values.

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